Implementing Lookup Tables

We mentioned earlier that calling the document() function with an empty string enabled us to access the nodes in the stylesheet itself. We can use this behavior to implement a lookup table. As an example, we’ll create a lookup table that associates an abbreviation such as ME with the state name Maine. We can then use the value from the lookup table as the sort key. More attentive readers might have noticed in our previous example that although the abbreviation MA does indeed sort before the abbreviation ME, a sorted list of the state names themselves would put Maine (abbreviation ME) before Massachusetts (abbreviation MA).

First, we’ll create our lookup table. We’ll use the fact that a stylesheet can have any element as a top-level element, provided that element is namespace-qualified to distinguish it from the xsl: namespace reserved for stylesheets. Here’s the namespace prefix definition and part of the lookup table that uses it:

<?xml version="1.0"?>
<!-- masterdox3.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:states="" exclude-result-prefixes="states"> <states:name abbrev="AL">Alabama</states:name> <states:name abbrev="AK">Alaska</states:name> <states:name abbrev="AS">American Samoa</states:name> <!-- Many state names deleted for brevity --> <states:name abbrev="WV">West Virginia</states:name> <states:name abbrev="WI">Wisconsin</states:name> <states:name ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.