Next / Previous / Contents / Shipman's homepage

7.5. xsl:key: Create an index to optimize input document access

The purpose of the xsl:key element is to define an index on information in the input file, so that the XSLT processor can retrieve that information more efficiently than by searching the entire file. This is admittedly an advanced feature, but can be very useful for larger projects.

You must give each key a name, using the name attribute, that will be used to refer to it later. Two more attributes describe which nodes to index, and what text to use as the index:

match

To define which nodes of the input document are indexed, use attribute match="m", where m is an XPath expression describing a node set.

use

To define what content strings are indexed, set this attribute to an XPath expression that describes some content relative to the nodes described by the match attribute.

For example, suppose you have a document with river elements that have a map attribute that you'll be referring to elsewhere. You might set up a key called river-map-key by placing this element inside your stylesheet:

    <xsl:key name="river-map-key" match="river" use="@map"/>