RE: [xsl] Trouble grouping with for-each-group and sort

Subject: RE: [xsl] Trouble grouping with for-each-group and sort
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 17 Mar 2010 18:11:14 -0000
I can't immediately see the answer, but I can see the problem:

<xsl:for-each-group select="current-group()" group-by="myns:E">
  <xsl:sort select="current-grouping-key()" data-type="number"/>
  <xsl:sort select="myns:G/myns:sub3/myns:inner" data-type="number"/>

The second sort key is only used when two things have the same primary sort
key. But by definition each group has a different current-grouping-key, so
this will never happen (remember at this level you are sorting the groups,
not the content of a particular group). 

I suspect that you want to use the second sort key to sort the contents of
current-group().
> 
>                         <xsl:call-template name="process-data" >
>                             <xsl:with-param name="data-set"
> select="current-group()"/>
>                         </xsl:call-template>
> 

You could do that within the process-data template, or you could do

<xsl:variable name="temp" as="item()">
  <xsl:perform-sort select="current-group()">
    <xsl:sort select="myns:G/myns:sub3/myns:inner" data-type="number"/>

and then pass the variable to the template.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

> -----Original Message-----
> From: mlcook@xxxxxxxxxx [mailto:mlcook@xxxxxxxxxx] 
> Sent: 17 March 2010 17:58
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Trouble grouping with for-each-group and sort
> 
> I have a collection of data that I'm trying to sort, but am 
> having difficulty with the for-each-group and sort operations.
> 
> The data is a series of elements of the form:
> 
> <DataItem xmlns="myns">
>     <A>2</A>
>     <B>1</B>
>     <C>6</C>
>     <D>4</D>
>     <E>740</E>
>     <F>99</F>
>     <G>
>         <sub1>202</sub1>
>         <sub2>0</sub2>
>         <sub3>
>             <inner>1.1</inner>
>         </sub3>
>     </G>
>     <H>
>         <sub1>202</sub1>
>         <sub2>262</sub2>
>         <sub3>
>             <inner>1.15</inner>
>         </sub3>
>     </H>
> </DataItem>
> 
> 
> where <G> and <H> have the same kind of data with the same 
> sub-element names.
> 
> The variable $list contains lots of DataItem elements.
> 
> Here's my current grouping and sorting attempt (from a larger
> transformation):
> 
>     <xsl:for-each-group select="$list/myns:DataItem" 
> group-by="myns:A">
>         <xsl:sort select="current-grouping-key()" data-type="number"/>
> 
>         <xsl:for-each-group select="current-group()" 
> group-by="myns:B">
>             <xsl:sort select="current-grouping-key()"
> data-type="number"/>
> 
>             <xsl:for-each-group select="current-group()"
> group-by="myns:C">
>                 <xsl:sort select="current-grouping-key()"
> data-type="number"/>
> 
>                 <xsl:for-each-group select="current-group()"
> group-by="myns:D">
>                     <xsl:sort select="current-grouping-key()"/>
> 
>                     <xsl:for-each-group select="current-group()"
> group-by="myns:E">
>                         <xsl:sort select="current-grouping-key()"
> data-type="number"/>
>                         <xsl:sort select="myns:G/myns:sub3/myns:inner"
> data-type="number"/>
> 
>                         <xsl:call-template name="process-data" >
>                             <xsl:with-param name="data-set"
> select="current-group()"/>
>                         </xsl:call-template>
> 
>                     </xsl:for-each-group>
>                 </xsl:for-each-group>
>             </xsl:for-each-group>
>         </xsl:for-each-group>
>     </xsl:for-each-group>
> 
> 
> When I run the transformation, all the groupings from the 
> for-each-group operations work fine. but the data is not sorted (it
> appears) at the innermost grouping according to the value of 
> "myns:G/myns:sub3/myns:inner" when the "process-data" 
> template is invoked.
> 
> All the other data is grouped and sorted fine, except for the 
> last sort on "inner".
> 
> Any ideas why the last sort doesn't appear to work?
> 
> Does a data-type of "number" for sorting correctly process 
> numbers with decimal points?
> 
> Am I using the wrong select on the parameter for process-data?
> 
> Is current-group() at that point not the sorted data that I 
> think it should be?
> 
> Is there a limit on nested for-each-group operations?
> 
> I'm using Oxygen 10.3 with Saxon SA 9.1.0.7.
> 
> Thanks, Mike
> 
> This email and any attachments are only for use by the 
> intended recipient(s) and may contain legally privileged, 
> confidential, proprietary or otherwise private information.  
> Any unauthorized use, reproduction, dissemination, 
> distribution or other disclosure of the contents of this 
> e-mail or its attachments is strictly prohibited.  If you 
> have received this email in error, please notify the sender 
> immediately and delete the original.

Current Thread