在XSLT 1.0:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text" encoding="utf-8" />
<!-- index ROWs by their @ref -->
<xsl:key name="kRowByRef" match="ROW" use="@ref" />
<!-- index ROWs by their @ref and @line -->
<xsl:key name="kRowByRefAndLine" match="ROW" use="concat(@ref, ',', @line)" />
<xsl:template match="/*">
<!-- 1) rows are processed with "ORDER BY @ref" -->
<xsl:apply-templates select="ROW" mode="ref-group">
<xsl:sort select="@ref" data-type="number" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="ROW" mode="ref-group">
<!-- 2) rows are grouped by @ref -->
<xsl:variable name="thisGroup" select="
key('kRowByRef', @ref)
" />
<xsl:if test="generate-id() = generate-id($thisGroup[1])">
<!-- 2.1) for the first item in the group,
nodes are processed with "ORDER BY @line" -->
<xsl:apply-templates select="$thisGroup" mode="line-group">
<xsl:sort select="@line" data-type="number" />
</xsl:apply-templates>
<!-- use a line as record separator -->
<xsl:text>---------------------------- </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="ROW" mode="line-group">
<!-- 3) rows are grouped by @ref, @line -->
<xsl:variable name="thisGroup" select="
key('kRowByRefAndLine', concat(@ref, ',', @line))
" />
<xsl:if test="generate-id() = generate-id($thisGroup[1])">
<!-- 3.1) for the first item in the group,
nodes are processed with "ORDER BY @type" -->
<xsl:apply-templates select="$thisGroup" mode="line">
<xsl:sort select="@type" data-type="number" />
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template match="ROW" mode="line">
<!-- 4) rows are printed out & appended with space or newline -->
<xsl:value-of select="@value" />
<xsl:choose>
<xsl:when test="position() = last()">
<xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
對於這種完全無序的輸入:
<data>
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
<ROW ref="0005631" type="00" line="1" value="John"/>
<ROW ref="0005631" type="01" line="1" value="Smith"/>
<ROW ref="0005632" type="04" line="2" value="Whiskey"/>
<ROW ref="0005632" type="02" line="1" value="Tennessee"/>
<ROW ref="0005631" type="02" line="1" value="Builder"/>
<ROW ref="0005632" type="01" line="1" value="Daniel's"/>
</data>
我得到:
John Smith Builder Australia
----------------------------
Jack Daniel's Tennessee
Whiskey
----------------------------
說明
此處發生的是分階段分組和排序,以便最終結果按@ref
,@line
和@type
進行分組和排序。所有分組均爲Muenchian分組。模板#1處理所有<ROW>
元素@ref
順序:
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005631" type="00" line="1" value="John"/>
<ROW ref="0005631" type="01" line="1" value="Smith"/>
<ROW ref="0005631" type="02" line="1" value="Builder"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
<ROW ref="0005632" type="04" line="2" value="Whiskey"/>
<ROW ref="0005632" type="02" line="1" value="Tennessee"/>
<ROW ref="0005632" type="01" line="1" value="Daniel's"/>
模板#2處理第一隻各@ref
組:
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
遞過整個組($thisGroup
)到模板#3分兩步,在@line
order:
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005631" type="00" line="1" value="John"/>
<ROW ref="0005631" type="01" line="1" value="Smith"/>
<ROW ref="0005631" type="02" line="1" value="Builder"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
<ROW ref="0005632" type="02" line="1" value="Tennessee"/>
<ROW ref="0005632" type="01" line="1" value="Daniel's"/>
<ROW ref="0005632" type="04" line="2" value="Whiskey"/>
模板#3取第一個eac ħ@line
組:
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
<ROW ref="0005632" type="04" line="2" value="Whiskey"/>
並處理它們,在三個步驟中發放整個組到模板#4,在@type
順序:
<ROW ref="0005631" type="00" line="1" value="John"/>
<ROW ref="0005631" type="01" line="1" value="Smith"/>
<ROW ref="0005631" type="02" line="1" value="Builder"/>
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
<ROW ref="0005632" type="01" line="1" value="Daniel's"/>
<ROW ref="0005632" type="02" line="1" value="Tennessee"/>
<ROW ref="0005632" type="04" line="2" value="Whiskey"/>
模板#4打印出來,附加空格或換行符在適當情況下。
所以你要求一個XSLT文檔?你能告訴我們你到目前爲止嘗試過什麼嗎? –