XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Keys and identifiers / Keys – runtime key lists

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)

Instruction xsl:key together with function key() catalogs XML documents by chosen criteria and can express ID-IDREF-like relationships. Like function id()-, the combination of xsl:key and key() speeds lookup of cataloged nodes. The processor chooses how to optimize access to the key index, for example sorting or indexing.

Declaring keys – xsl:key

Element xsl:key declares a key; attribute name gives its name, which must be a QName and may therefore have a namespace prefix.

A key is a catalog storing document nodes, usually elements, and associated key values.

An associative-array analogy is imperfect but useful. A key has three components:

  • The name:
    The key name, defined by attribute name on xsl:key, and used by function key() to access the list.
    Array equivalent: the array name.

  • The pointers:
    A list of pointers to all nodes in the match node set. Each key value has one pointer. Depending on the result of the use expression, several pointers may address one node.
    Array equivalent: the contents of its slots.

  • The key values:
    A list of key values derived by evaluating the use expression over the sequence from the match (pattern ). The processor stores and optimizes this list, for example by sorting. These values and their pointers provide the functionality of xsl:key and key(). A key value addresses the list externally, like an array slot name returning the desired pointer.

Note: Pointers from several independent key lists may refer to the same node without error.

Accessing keys – function key()

Function key() finds nodes whose named key has a supplied value. It takes two arguments: the index name and the criterion defining the desired nodes, the key value:

key('schlüsselname','schlüsselwert')

Using xsl:key, a key must first be declared for a node related to the element identified by attribute match before key() can apply. Attribute use relates the element to its key value. The function compares all cataloged values with the requested value.

This declaration makes attribute isbn the key for all elements <buch> and uses isbn-nummer as the key name:

<xsl:key name="isbn-nummer" match="buch" use="@isbn" />

For a large document containing this element:

<buch isbn="3-899-77610-5"> ... </buch>

find the book using function key() and this expression:

key('isbn-nummer', '3-899-77610-5')

This can be more efficient than:

//buch[@isbn='3-899-77610-5']

It resembles database-table indexing, which can greatly accelerate a query.

Function key() excels when many elements must be examined. The XSLT processor creates an index of elements identified by declaration xsl:key for quick access.

An XPath predicate finds the same result more slowly: it must test every candidate, whereas the key declaration lets the parser fetch the matching index pointer directly.

Beispiel

Suppose a document contains glossary definitions <definition> in the form

<definition term="der_term">
  ... Beschreibung von der_term ...
</definition>

and text markers <referenz> referring to definitions by name:

... <referenz>der_term</referenz> ...

This stylesheet links references and definitions:

<!-- eine Biblithek aller Definitionen bilden: -->
<xsl:key name="ref" match="definition" use="@term"/>
<!-- Link zur Definition in Fließtext einbauen: -->
<xsl:template match="referenz">
  <b>
    <!-- die Definition mit key() suchen: -->
    <a href="#{generate-id(key('ref',.))}"><xsl:apply-templates/></a>
  </b>
</xsl:template>
<xsl:template match="definition">
  <p id="{generate-id()}"><b>Definition: </b><xsl:apply-templates/></p>
</xsl:template>

Calling key() inside generate-id() returns the element associated with current element <referenz> through key ref → definition. The current node’s string value <referenz> is the comparison value. Function generate-id() is applied to it. A second call to generate-id() returns the same value, used as the link reference.

   

back to top