Re: [xsl] XPath expression that yields the same result as xsl:for-each-group?

Subject: Re: [xsl] XPath expression that yields the same result as xsl:for-each-group?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 30 May 2019 13:20:40 -0000
On 30.05.2019 15:06, Martin Honnen martin.honnen@xxxxxx wrote:

What XPath expression will yield the desired value for $groups?

<xsl:variable name="groups" select="???" />

Is there an XPath 2.0 expression that can be used?

What kind of sequence type would you expect that XPath expression to return? You can't build sequences of sequences to somehow wrap the items belonging to a group.

So unless you could move to XPath 3.1 with maps and/or arrays I don't
see an XPath type constructed with pure XPath to hold groups. And of
course even there it would be easier and smarter to use XSLT to create
the map or the nested sequence/array or array/sequence structure than to
rely on pure XPath I think.

A pure XPath 3.1 expression to build the groups as a sequence of array(element(row)) would be

let $keys := distinct-values($rows/concat(ARPT__IDENT, '|', TRM__IDENT))
                    return
                      for $key in $keys
                      return array { $rows[$key = concat(ARPT__IDENT,
'|', TRM__IDENT)] }


you could use as



<xsl:template match="Test"> <xsl:variable name="rows" select="row" as="element(row)*"/> <xsl:variable name="groups" as="array(element(row))*" select="let $keys := distinct-values($rows/concat(ARPT__IDENT, '|', TRM__IDENT)) return for $key in $keys return array { $rows[$key = concat(ARPT__IDENT, '|', TRM__IDENT)] }"/> <results> <xsl:for-each select="$groups"> <group> <xsl:sequence select="?*" /> </group> </xsl:for-each> </results> </xsl:template>

https://xsltfiddle.liberty-development.net/jyRYYiQ

But I think using XSLT 2/3 grouping to build a grouping structure, be it
an XML one in XSLT 2 or such a nested sequence of arrays if needed in
XSLT 3 is much easier and probably more efficient anyway.

Current Thread