One frequent requirement is grouping. Grouping is the act of sorting things into a certain order and then coalescing the data for all things that have the same value for the sort key. For example, if you have data for a library of books, you might use grouping to create a report of books sorted by author, with a pretty visual break between authors and a count of books by each author.
In XML, we sort XML elements and use as the sort key the value of an attribute or subordinate element.
XSLT version 2 will have grouping as a native feature, but most of us are using version 1 XSLT processors. So how do we do grouping?
There are many techniques to achieve grouping, many of which are rather brute-force approaches. However, the Muench method is perhaps the most flexible grouping method to be devised for XSLT version 1. The Muench method is named after Steve Muench, who developed this method as the Oracle XML Evangelist.
For example, let's use this XML document taken from a recent thread. The Tek-Tips poster wanted to group entries based on the value of xpos so that he could report all of the ypos values for each xpos.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Add an index (key) that allows selection based on the value of xpos -->
<xsl:key name="X" match="Table" use="xpos"/>
<!-- The outer for-each selects the first Table node for every unique xpos value.
In other words, the outer for-each will iterate exactly once for each unique
value of xpos. -->
<xsl:for-each select="//Table[generate-id(.)=generate-id(key('X', xpos))]">
<!-- The inner for-each iterates over all nodes that match the outer for-each xpos value -->
<xsl:for-each select="key('X', xpos)">
Obviously it is crucial that there be careful matching between the attributes in the xsl:key element and the xsl:for-each and xsl:sort elements.