Ian's books | CPU Magazine

XSLT Part 12: The Muenchian Method: advanded features with key() and generate-id()

As described in the December 2003 issue of CPU - Computer Power User magazine.


A) Downloadable ZIP File

B) The Data and Stylesheets

More about the Muenchian Method and Advanced XSLT

The Muenchian method can be hard to appreciate without a good understanding of the XML data model and XSLT indexes. It can also be extended to cover more complex cases than the one described here! If you're still confused -- or just want to learn more -- have a look at some of the 'on the web' explanations. Jeni Tennison has a nice illustration at www.jenitennison.com/xslt/grouping/muenchian.html. Another good source is an article by Elliotte Rusty Harold, at www.ibiblio.org/xml/books/xmljava/chapters/ch04s06.html. There's also a nice section on Muenchian (and other advanced) methods in the XSLT FAQ, at www.dpawson.co.uk/xsl/sect2/muench.html.

Last, you may feel you're still confused over how key()s and indexes work. In this case, have a look at Ken Holman's complete (and nicely illustrated) description of keys, at www.cranesoftwrights.com/resources/xslkeys/index.htm

What's Next for XPath and XSLT?

This article concludes our 12 part series on XPath and XSLT, versions 1.0. However, work is actively underway on Versions 2.0 of both, so this is a good time to highlight of the new features that will soon be coming along.

XPath 2.0 will bring a bunch of new features. Notably there are new expressions supporting grouping of nodes. There will also be support for defining sequences of strings (e.g. "string1" followed by "string2") or ranges of number ("1 to 5") that again make selecting nodes far easier, particularly in the context of XSLT. Also, unlike XPath 1.0, XPath 2.0 is 'schema aware' -- XPath 2.0 expressions will access and process information (e.g. elements and or attributes) added into an XML data set by an XML schema, or intrinsically defined by the schema data model.

In terms of expression structure, XPath 2.0 is syntactically similar to XPath 1.0, so XPath 1.0 expressions are still valid to an XPath 2.0 processor. However, XPath 2.0 defines somewhat different processing rules, so that some XPath 1.0 expressions will evaluate differently in an XPath 2.0 environment. XPath 2.0 provides a compatibility mode switch for backward-compatibility with XPath 1.0: when enabled, an XPath 2.0 processor will behave as much like an XPath 1.0 processor as possible. However, compatibility is not perfect, and the XPath 2.0 specification discuses where and why compatibility fails.

XSLT 2.0 -- in addition to supporting XPath 2.0 -- has many other important changes, including support for XML schemas, support for element grouping (goodbye Muenchian Method!), and - hopefully - the ability to define explicit types of data expected out of particular templates. The latter is particularly useful, as the stylesheet can stop processing and flag an error when a template starts generating data that is not expected. One of the main difficulties working with XSLT 1.0 stylesheets is that they can fail for unknown reasons, and it can be hard finding the template causing the problem. With XSLT 2.0 the processor will (hopefully) flag an error and stop part way through, and will indicate which template caused the problem, significantly aiding the debugging process.

XPath 1.0 and XSLT 2.0 are still in working draft status (see www.w3.org/TR/xpath20/ and www.w3.org/TR/xslt20/), but several experimental XSLT processors (in particular, Michael Kay's Saxon 7.6.x, from saxon.sourceforge.net) support the current drafts. So, if you want to play around with the latest and greatest technology, why not download Saxon 7.6.x and give it a whirl?