O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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="http://www.w3.org/1999/XSL/Transform" xmlns:states="http://www.usps.com/ncsc/lookups/abbreviations.html" 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required