I have the below xml which I need to first group-by Requester and then for each group I need to have another group-by based on Item exists or not.
<root>
<row>
<Requester>Tim</Requester>
<Item>A</Item>
</row>
<row>
<Requester>Tim</Requester>
<Item>B</Item>
</row>
<row>
<Requester>Tim</Requester>
<Item/>
</row>
<row>
<Requester>Ken</Requester>
<Item>A</Item>
</row>
As you can see the first 3 rows have matching requester but 3rd row for Tim doesn't have Item. So there should be 2 groups for Tim.
The output I need looks like:
<root>
<row>
<Requester>Tim</Requester>
<Line>
<Item>A</Item> ---> Grouped together because Item exists. Item name may not match
<item>B</item>
</Line>
</row>
<row>
<Requester>Tim</Requester>
<Line>
<Item/> --> Separate group because there is no Item
</Line>
</row>
<row>
<Requester>Ken</Requester>
<Line>
<Item>A</Item>
</Line>
</row>
The xslt I was able to build so far:
<xsl:template match="/">
<root>
<xsl:for-each-group select="root/row" group-by="Requester">
<row>
<Requester>
<xsl:value-of select="Requester[position()=1]"/>
</Requester>
<xsl:for-each select="current-group()">
<Line>
<Item>
<xsl:value-of select="Item"/>
</Item>
</Line>
</xsl:for-each>
</row>
</xsl:for-each-group>
</root>
</xsl:template>
Try:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<root>
<xsl:for-each-group select="row" group-by="Requester, exists(Item/text())" composite="yes">
<row>
<xsl:copy-of select="Requester"/>
<Line>
<xsl:copy-of select="current-group()/Item"/>
</Line>
</row>
</xsl:for-each-group>
</root>
</xsl:template>
</xsl:stylesheet>