Re: [xsl] Sorting seems not to work completely

Subject: Re: [xsl] Sorting seems not to work completely
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Wed, 27 Jul 2011 19:30:55 +0100
When you use xsl:sort as a child of xsl:for-each-group, it defines how the groups are sorted, not how the values are sorted within each group. You need to use an <xsl:for-each select+"current-group()"> with an xsl:sort child to sort the items within the group.

Michael Kay
Saxonica

On 27/07/2011 19:19, Mark wrote:
Thanks Martin,
I believe I am using the free version of Saxon 9 (but did not know how to obtain the version number directly) called in a windows batch file with the command :


java -jar c:\Users\HP\saxon\saxon9.jar xml\all-themes.xml xslt\get-dates.xsl > work\dates.xml

The template, a selection of the input file, and the corresponding selection from the output file appear below.
Thanks,
Mark


Again, my template is:

<xsl:template match="List">
<xsl:element name="List">
<xsl:for-each-group select="Item/Stamp" group-by="concat(Date/@year, ' ', Date/@month, ' ', Date/@day)">
<xsl:sort select="Date/@year" data-type="number"></xsl:sort>
<xsl:sort select="Date/@month" data-type="number"></xsl:sort>
<xsl:sort select="Date/@day" data-type="number"></xsl:sort>
<xsl:sort select="@value" order="descending" data-type="number"></xsl:sort>
<xsl:element name="Item">
<xsl:copy-of select="current()/Date" copy-namespaces="no"></xsl:copy-of>
<xsl:copy-of select="current-group()" copy-namespaces="no"></xsl:copy-of>
</xsl:element>
</xsl:for-each-group>
</xsl:element>
</xsl:template>


A suitable selection of the input file [edited to remove <Stamp>'s numerous sibling elements not involved in this process]:

<List>
<Item>
<Stamp scott-number="2888" pofis-number="12" value="1" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="CeskC) Budejovice" eng="Ceske Budejovice"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2889" pofis-number="13" value="2" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="CstC- nad Labem" eng="Usti nad Labem"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2890" pofis-number="14" value="3" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="CeskC= Crumlov" eng="Cesky Crumlov"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2892" pofis-number="15" value="5" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="PlzeE" eng="Pilzen"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2895" pofis-number="16" value="8" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Olomouc" eng="Olomouc"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2896" pofis-number="17" value="10" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Hradec KrC!lovC)" eng="Hradec Kralove"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2897" pofis-number="18" value="20" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Praha" eng="Prague"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<Item>
<Stamp scott-number="2898" pofis-number="19" value="50" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Opava" eng="Opava"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
</List>


The complete output for the sample input. My output is not in descending according to the value attribute:
<List>
<Item>
<Date year="1993" month="7" day="1"></Date>
<Stamp scott-number="2888" pofis-number="12" value="1" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="CeskC) Budejovice" eng="Ceske Budejovice"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2889" pofis-number="13" value="2" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="CstC- nad Labem" eng="Usti nad Labem"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2890" pofis-number="14" value="3" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="CeskC= Crumlov" eng="Cesky Crumlov"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2892" pofis-number="15" value="5" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="PlzeE" eng="Pilzen"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2895" pofis-number="16" value="8" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Olomouc" eng="Olomouc"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2896" pofis-number="17" value="10" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Hradec KrC!lovC)" eng="Hradec Kralove"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2897" pofis-number="18" value="20" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Praha" eng="Prague"></Inscription>
<Theme>Cities</Theme>
</Stamp>
<Stamp scott-number="2898" pofis-number="19" value="50" type="definitive">
<Date year="1993" month="7" day="1"></Date>
<Inscription czech="Opava" eng="Opava"></Inscription>
<Theme>Cities</Theme>
</Stamp>
</Item>
<List>


-----Original Message----- From: Martin Honnen
Sent: Wednesday, July 27, 2011 10:40 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Sorting seems not to work completely

Mark wrote:
I cannot get the @value to sort in descending order. My XML (simplified)
and template are below. The sorts on the Date attributes work, but the
one on the value attribute does not. Please, what am I doing wrong?

I don't see anything wrong with the snippets you posted. You might want to provide more details such as an input sample you are trying to group and sort, the result you get, the one you want, and details of the XSLT processor you used, so that we can reproduce the problem.

Current Thread