Name

key() Function — References a relation defined with an <xsl:key> element. Conceptually, the key() function works similarly to the id() function, although keys are more flexible than IDs.

Synopsis

node-set key(
               string
               object
               )

Inputs

The name of the key (defined by an <xsl:key> element) and an object. If the object is a node-set, then the key() function applies itself to the string value of each node in the node-set and returns the node-set of the result of all those key() function invocations. If the object is any other type, it is converted to a string as if by a call to the string() function.

Output

A node-set containing the nodes in the same document as the context node whose values for the requested key match the search argument(s). In other words, if our stylesheet has an <xsl:key> element that defines a key named postalcodes based on the <postalcode> child of all <address> elements in the current document, the function call key(postalcodes, '34829') returns a node-set containing all the <address> elements with a <postalcode> element whose value is 34829.

Defined in

XSLT section 12.2, Keys.

Example

To illustrate the power of the key() function, we’ll use this document—a truncated version of the glossary we discussed in Chapter 5:

<?xml version="1.0" ?> <glossary> <glentry> <term id="applet">applet</term> <defn topic="Java" language="en"> An application program, written in the Java programming language, that can be retrieved from a web server and executed by a web browser. A reference ...

Get XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.