Search code examples
xslt-2.0xpath-2.0

XSLT2 for-each-group: is there a way to reference sequence of representative elements?


Inside a for-each-group statement in XSLT2 the focus changes to a set of representative elements, one from each group. This means that, for example, last() returns the number of groups (because that equals the number of representative elements, and hence the "size" of the focus). Position() returns (essentially) the group number because it is the position of the representative element for the group under discussion within the sequence of representative elements, etc.

My question is whether it is possible to reference the set of those representative elements in an Xpath2 statement. Something like current-group() except containing the set of all representative elements (one from each group) rather than the set of all elements from the current group.


Solution

  • Well inside the for-each-group you process each group and not all groups. Thus to find the first item in all groups you would need

    <xsl:variable name="reps" as="node()*">
      <xsl:for-each-group select="foo" group-by="bar">
        <xsl:sequence select="."/>
      </xsl:for-each-group>
    </xsl:variable>
    

    in my understanding.