File: Data.xml
<?xml version="1.0"?>
<addressbook>
<address>
<first-name>Doris</first-name>
<last-name>Smith</last-name>
<city>New York</city>
<state>WI</state>
</address>
<address>
<first-name>Mary</first-name>
<last-name>Smith</last-name>
<city>Vancouver</city>
<state>MA</state>
</address>
</addressbook>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>Customers grouped by state

</xsl:text>
<xsl:for-each-group select="/addressbook/address" group-by="state">
<xsl:sort select="state"/>
<xsl:text> State = </xsl:text>
<xsl:value-of select="current-grouping-key()"/>
<xsl:text>
</xsl:text>
<xsl:for-each select="current-group()">
<xsl:text>	</xsl:text>
<xsl:value-of select="(first-name, last-name)" separator=" "/>
<xsl:text>, </xsl:text>
<xsl:value-of select="city"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Output:
Customers grouped by state
State = MA
Mary Smith, Vancouver
State = WI
Doris Smith, New York
5.53.Grouping |
| 5.53.1. | for each group |