Home Programming XSLT Grouping and Sorting Example (Muenchian Method)

Search

XSLT Grouping and Sorting Example (Muenchian Method) Print
Written by Chris Gountanis   

Sorting distinct groups with XSLT 1.0 is a common issue. When XSLT 2.0 is standardized there will be for-each-group tags available which will replace this example. The code below will show you how to group and sort within each group utilizing an XML data file. The XSL code will group by material then sort by price within each distinct material. To add some additional functionality a choose tag was included in order to style the low quanity values.

 

The Muenchian Method is a method developed by Steve Muench for performing these functions in a more efficient way using keys. Keys work by assigning a key value to a node and giving you easy access to that node through the key value. If there are lots of nodes that have the same key value, then all those nodes are retrieved when you use that key value. Effectively this means that if you want to group a set of nodes according to a particular property of the node, then you can use keys to group them together.

 

XSL CODE

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="widgets-by-material" match="widgets" use="material" />

<xsl:template match="dataroot">

 

<xsl:for-each select="widgets[count(. | key('widgets-by-material',material)[1]) = 1]">

<xsl:sort select="material" />

<xsl:for-each select="key('widgets-by-material', material)">

<xsl:sort select="price" data-type="number" />

<xsl:choose>

<xsl:when test="quantity < 25">

<td id="quantityreorder">

<xsl:value-of select="quantity" />

</td>

</xsl:when>

 

<xsl:otherwise>

<td id="quantity">

<xsl:value-of select="quantity" />

</td>

</xsl:otherwise>

</xsl:choose>

</xsl:for-each>

</xsl:for-each>

 

</xsl:template>

</xsl:stylesheet>

 

XML CODE

<dataroot>

<widgets>

<id>1</id>

<material>Aluminum</material>

<length>1</length>

<width>2</width>

<thickness>0.25</thickness>

<price>1</price>

<quantity>345</quantity>

</widgets>

</dataroot>

 

 

CSS CODE

#material {

background-color: #99CC00;

text-align: center;

padding: 8px;

font-weight: bolder;

}

#quantityreorder {

background-color: #CC0000;

font-weight: bold;

text-align: right;

font-style: oblique;

}

 

 

OUTPUT

XSLT Grouping and Sorting Example (Muenchian Method)

 

DOWNLOADS

Sample Code

Updated 02-07-2009: Recently added to the sample code is another Extensible Stylesheet Language (XSL) file that displays the results grouped by material and then by width as well. The standard example above only groups by material while displaying the width as a column for each record. Thanks to Aigars for for sending me this advanced group by code sample. The sample code link above contains both examples.

 

WORKS SITED

Jeni's Grouping Using the Muenchian Method

Retrieved From: http://www.jenitennison.com/xslt/grouping/muenchian.html

Last Updated on Saturday, 07 February 2009 13:50