XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / key

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

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The top-level element xsl:key declares a key for later reference in XPath patterns and expressions through the XSLT function key().

Classification

Declaration

Function group

Indexing source documents

Introduction

XSLT 1.0

Position in the stylesheet and permitted contents:

xsl:key is a top-level element, directly inside xsl:stylesheet or xsl:transform. Any number of keys may be declared.

Since XSLT 2.0, the element may contain an optional sequence constructor, an alternative to the optional use attribute, to generate key values for nodes identified by match. In XSLT 1.0, the element is always empty.

Attributes:

Standard attributes apply to xsl:key. The element also has two required attributes, match and name, defining the key name and corresponding key. Two further attributes, collation (new in XSLT 2.0) and use, are optional. Attribute use is an alternative to the element’s sequence constructor. In XSLT 1.0, use is required because content is forbidden.

collation

Value

uri string

Usage

Optional

Introduction

XSLT 2.0

The collation attribute identifies a file defining string comparison rules, if needed to compare key values correctly. Without a specified collation, the default is used.

Note: Unlike its usual use, the collation attribute cannot, because xsl:key is a declaration, use an attribute value template. If a key combines several same-named declarations, perhaps imported from different modules, all must use the same collation. Otherwise a static error occurs (ERR XTSE1220).

Note: This attribute does not exist in XSLT 1.0.

match

Value

xpath-pattern

Usage

Required

Introduction

XSLT 1.0

The match attribute takes an XPath pattern defining the node sequence to index. Indexing reads every node matching the match attribute, forming the key’s underlying catalogue.

name

Value

QName

Usage

Required

Introduction

XSLT 1.0

The name attribute must contain a QName, which may have a namespace prefix. The name identifies the key when referenced.

use

Value

xpath-expression

Usage

Optional ( required in XSLT 1.0)

Introduction

XSLT 1.0 (changed in XSLT 2.0)

The use attribute contains an XPath expression evaluated for each source node matching the match expression. The result can be a sequence, a node set in XSLT 1.0, or a string. In the simpler string case, it becomes the key value.

If the use expression produces a sequence, for example by selecting several child elements of the catalogue node, each item in the sequence provides a key value through its string value, by atomisation. The catalogue node then has several key values (a multi-valued key).

Note: The use attribute may only be used as an alternative to a sequence constructor inside xsl:key. It is required when the element is empty and must be omitted when a sequence constructor is present. Violations are static errors (ERR XTSE1205).

Purpose:

The declaration xsl:key defines a key, with the name attribute providing its identifier.

A key (key) is a catalogue of source nodes, usually elements, and associated key values. The processor builds this catalogue while reading documents. XPath expressions and patterns in the stylesheet can use it through the key() function.

The XSLT function key() takes two arguments, key name and key value, together forming a key-value-pair. Finding indexed nodes is much faster, especially in complex XML documents, than finding the same nodes at runtime with an XPath expression.

Unlike an identifier, a key need not be unique. A key may reference several nodes, and a node may have several keys referring to it.

No conflicts in imported or included xsl:key declarations
Same-named key declarations introduced by import or inclusion add their key values together. Import precedence does not apply (see xsl:import and xsl:include).

Declaring and calling a key:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:key name="angestellte" match="mitarbeiter" use="@abteilung"/>
  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="key('angestellte', 'verkauf')">
          <p><b>Name: </b><xsl:value-of select="name"/>
            <br/>
            <b>Geb.: </b><xsl:value-of select="geb"/>
            <br/>
            <b>Gehalt: </b><xsl:value-of select="gehalt"/>
            <br/>
            <b>Fon: </b><xsl:value-of select="fon"/>
            <hr/>
          </p>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Under the identifier "angestellte", a key table of all employees by department is created and stored. To reference it, the function key() receives the key name and the value to check, here the department name from attribute abteilung. The result is a sequence of all employee elements whose department attribute has that value.

Element definition:

XSLT 1.0:

<!-- Category: top-level-element -->
<xsl:key 
     name = qname 
     match = pattern 
     use = expression 
/>

XSLT 2.0:

<!-- Category: declaration -->
<xsl:key
     name = qname
     match = pattern
     use? = expression
     as? = qname
     collation? = uri-string >

<!-- Content: sequence-constructor -->
</xsl:key>

Note: The use attribute is optional in the XSLT 2.0 signature!

   

  

back to top