More XSLT & XPath grouping
page 1 of 1
Published: 26 Sep 2003
Unedited - Community Contributed
This is a follow-up article for my "Simple XSLT & XPath grouping" article. This time I demonstrate a bit more effective way to do the grouping and also how to utilize the Muenchian grouping technique.
by Teemu Keiski
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 11363/ 31

The article

Sample data

Sample XML data that both examples utilize is as follows. It is exactly same as in the previous article.

<?xml version="1.0" encoding="utf-8" ?>
           <car manufacturer="Mazda" owner="Jason" />
           <car manufacturer="Opel" owner="Teemu" />
           <car manufacturer="Toyota" owner="Thomas" />
           <car manufacturer="Opel" owner="Henry" />
           <car manufacturer="Daimler" owner="Mika" />
           <car manufacturer="Toyota" owner="Colt" />
           <car manufacturer="Toyota" owner="Teemu" />
           <car manufacturer="Daimler" owner="Jason" />

[Download sample data]

Selecting distinct nodes more effectively

If nodes to be selected are in sorted order, we could use following XPath expression to get distinct nodes.


In other words, we don’t have to check all preceding sibling nodes, if we know that nodes are in sorted order. In this case we need to check only the immediate preceding sibling. This is usually more effective way than checking all preceding siblings, although performance boost is dependant on processor implementation.

So as a result one way to utilize this approach is to first sort the nodes before selection. Example related to this demonstrates also usage of node-set() extension function so that we are able to use temporary tree as node-set in XSLT 1.0. To remind the reader, XSLT processor implemented in .NET Framework 1.0 supports XSLT version 1.0.

[Download XSLT stylesheet]

Grouping by manufacturers utilizing Muenchian technique

Muenchian grouping is technique that utilizes XSLT keys and XPath generate-id() function to do the grouping. First we generate the key in XSLT stylesheet:

<xsl:key name="car-key" match="car" use="@manufacturer"   />

As a result, call to XPath function:


would return all car elements that have 'Toyota' as value of @manufacturer attribute. Therefore:


would return the first car that has ‘Toyota’ as manufacturer. And again therefore expression:


would be true only for car that is the first car from this manufacturer. The task of generate-id() function is to generate unique identification string for given node. And because generate-id() function actually uses only the first node in the node-set, predicate [1] would not be needed at all. Therefore unique manufacturers could be selected as follows:


Muenchian grouping is usually faster than the approach using preceding-sibling axis because XSLT keys would be implemented utilizing some kind of index or hash table and therefore selecting  nodes with same key value would be fast. Technique gives also other solution possibilities to be used with XSLT.

[Download XSLT stylesheet]


I have demonstrated with couple of more advanced examples how selecting distinct nodes and grouping can happen. You should be able to utilize this in your own stylesheets easily.

Jeni Tennison's XSLT Pages
XSLT Programmer's Reference 2nd Edition, Wrox Press

Idea for this article by
Kirk Allen Evans,

User Comments

Title: Cryptic   
Name: Anon
Date: 2007-10-03 6:12:33 PM
Wow, this really needs some more explaination as it is very cryptic at best.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-28 7:44:05 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search