Re: [xsl] Sorting seems not to work completely

Subject: Re: [xsl] Sorting seems not to work completely
From: "Mark" <mark@xxxxxxxxxxxx>
Date: Wed, 27 Jul 2011 12:49:50 -0700
Thanks Andrew,
Your template one scrambles the <Date> order. Have not investigated why as Murray's template worked almost perfectly. Thanks for the <Group...> idea. It will make a nice debugging feature for me in the future.
Mark


-----Original Message----- From: Andrew Welch
Sent: Wednesday, July 27, 2011 12:24 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Sorting seems not to work completely


On 27 July 2011 19:59, Mark <mark@xxxxxxxxxxxx> wrote:
Hello Michael,
I'm not sure I understood the '+' in  <xsl:for-each
select+"current-group()">

typo... :)


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.

The code below does what you need. Directly within the for-each-each group the context is the sequence of <Stamp> elements all with the same year/month/day. The sort there sorts on that value. Then the for-each inside there processes that seqeunce (that all have the same year/month/day one at a time, sorted by the value. I've used a different wrapper element for each group to help show what is going on.

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


-- Andrew Welch http://andrewjwelch.com

Current Thread