xsl-list
[Top] [All Lists]

Re: [xsl] Sorting seems not to work completely

2011-07-27 13:59:32
Hello Michael,
I'm not sure I understood the '+' in <xsl:for-each select+"current-group()">

I tried the below template. [1] gives me the <Stamp> elements grouped under <Date> but not sorted by value. [2] gives me the <Stamp> elements sorted by value but not grouped under <Date>. Clearly I am missing something as I am quite week in XPath. Where am I going wrong.
Mark

<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:for-each select="current-group()">
<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> <!--[1] <xsl:copy-of select="current-group()" copy-namespaces="no"></xsl:copy-of> --> <!--[2]<xsl:copy-of select="current()" copy-namespaces="no"></xsl:copy-of>-->
         </xsl:element>
       </xsl:for-each>
     </xsl:for-each-group>
   </xsl:element>
 </xsl:template>
-----Original Message----- From: Michael Kay
Sent: Wednesday, July 27, 2011 11:30 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Sorting seems not to work completely

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="Ceské 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="Ústí 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="Ceský 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="Plzeň" 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 Králové" 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="Ceské 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="Ústí 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="Ceský 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="Plzeň" 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 Králové" 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(_at_)lists(_dot_)mulberrytech(_dot_)com
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.



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--