RE: [xsl] current-group()[1] within xsl:for-each-group

Subject: RE: [xsl] current-group()[1] within xsl:for-each-group
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 2 Mar 2005 20:45:18 -0000
The nodes within each group should be in "population order", that is, the
order of the original sequence, which in this case is document order.

I notice that you are doing something a little unusual, you are sorting the
groups using something other than the grouping key. The sort key (for
sorting the groups) mb3e:prim_sort_key is evaluated against the first item
in each group - if its value differs from one member of the group to another
this could be quite confusing.

The xsl:sort within the apply-templates should affect the order in which the
items within each group are processed, but it doesn't affect the result of
current-group() - at least, it shouldn't!

Michael Kay 

> Now if the unintended output had come from applying the matching
> template to the group's first node (in terms of document 
> order), I would
> have immediately realized that.  But for some reason the nodes in the
> group are not in document order.  Why is that?

As I say, I think they should be in document order as far as current-group()
is concerned; but not processed in document order, because of the xsl:sort
within apply-templates.
> Here is a more concrete question: Would it make a difference
> semantically or performance-wise if I changed this
>       <xsl:for-each-group select="mb3e:document" 
> group-by="mb3e:fam_id">
> to this
>       <xsl:for-each-group select="mb3e:document" 
> group-by="text(mb3e:fam_id)">

There's no text() function - you probably meant string() or data() - but
either way, you're only doing explicitly what the system is doing anyway.
> or is that effectively what is done when the each node's grouping key
> sequence is atomized and the resulting values compared?
> > >   <xsl:variable name="structured-number"
> > >                 select="if (position() = 1)
> > >                         then $family-structured-number
> > >                         else esd:structured-number(.)"/>
> > 
> > Where is this variable declared, and where is it used? The 
> reference to
> > position() makes it highly context-sensitive. 
> As I tried to express, it is declared within the xsl:template that is
> applied above (within xsl:for-each-group).  position() seems 
> to work as
> I assumed, returning the matched node's position within the group.

OK, I understand now. It should return the position in the actual order of
processing, that is, the sorted order.
> But again I wonder: Would it make a difference if I changed . (dot) to
> current():
>   <xsl:variable name="structured-number"
>                 select="if (position() = 1)
>                         then $family-structured-number
>                         else esd:structured-number(current())"/>

No, in this context . and current() are synonyms.

Michael Kay

Current Thread