Grouping with the Muenchian method
For those not aware, the XSLT 1.0 spec does not include any (obvious) built-in capabilities for grouping effeciently.
It should be noted, this has been resolved in XSLT 2.0, and if you have the choice, use it!
Unfortunately, Microsoft has yet to adopt XSLT 2.0 (as of the .Net 3.0 framework). Though I hear it may be included in 4.0. (check out the XML team's blog for updates - http://blogs.msdn.com/xmlteam/default.aspx)
There are other .Net assemblies which provide this, such as Saxon project on CodePlex (http://saxon.sourceforge.net/)
I add effeciently because there is an approach using a significant amount of XPath and subquerying that can work, but for large datasources this can be computationally intensive.
Thankfully, one man (Steve Muench) realized that the built-in support for Keys (keyed dictionary) will effectively provide this functionality, in a round-about method. And thus was born, the Muenchian Method for grouping in XSLT.
In this example, for each category, I create a table. In the table, I create a row for each item in the category.
More info can be found at:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:z="#RowsetSchema">
<xsl:key name="CoursesByCategory" match="/z:rows/z:row" use="@Course_x0020_Category" />
<xsl:for-each select="z:row[count(. | key('CoursesByCategory', @Course_x0020_Category)) = 1]">
<xsl:sort select="@Course_x0020_Category" />
Category: <xsl:value-of select="@Course_x0020_Category" />
<table width="100%" style="margin: 5px 0px 0px 5px;">
<xsl:for-each select="key('CoursesByCategory', @Course_x0020_Category)">
<xsl:sort select="@Title" />
<xsl:value-of select="@ID" />
<xsl:value-of select="@Description" disable-output-escaping="yes" />