In 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>
For this completely unordered input:
<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>
I get:
John Smith Builder Australia
----------------------------
Jack Daniel's Tennessee
Whiskey
----------------------------
Explanation
What happens here is a staged grouping and sorting, so that the eventual result is grouped and ordered by @ref
, @line
, and @type
.
All of the grouping is Muenchian grouping. The template #1 processes all <ROW>
elements in @ref
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="04" line="2" value="Whiskey"/>
<ROW ref="0005632" type="02" line="1" value="Tennessee"/>
<ROW ref="0005632" type="01" line="1" value="Daniel's"/>
Template #2 processes the first of each @ref
group only:
<ROW ref="0005631" type="04" line="1" value="Australia"/>
<ROW ref="0005632" type="00" line="1" value="Jack"/>
handing the entire group ($thisGroup
) over to template #3 in two steps, in @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"/>
Template #3 takes the first of each @line
group:
<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"/>
and processes them, handing the entire group over to template #4 in three steps, in @type
order:
<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"/>
Template #4 prints them out, appending spaces or newlines where appropriate.