This example uses both keys and modes.
Every 'computer' element in my inventory has a 'vendor' by which I want to group:
Define the key:
<xsl:key name="vendors" match="computer" use="vendor"/>
Find the unique key values:
<xsl:apply-templates select="computer[count(.|key('vendors',vendor)[1])=1]">
Process the unique value:
<xsl:template match="computer"> <vendor> <xsl:apply-templates select="vendor"/> <!-- Now we access the full set of elements that match the key value. These will be copied across in a special "identity" mode. --> <xsl:apply-templates select="key('vendors',vendor)" mode="computer-copy"/> </vendor> </xsl:template>
Full Example: inventory.xml group-by.xsl