Chapter 9. Identifiers and Keys

XSLT and XPath between them provide several ways to locate items, and to index and link them using identifiers and keys that express relationships between elements.

The id() function in XPath 2.0 enables you to find element nodes having attributes of type xs:ID, and it can be useful when the source schema or DTD is accessible. However, there are some limitations that result from the rules set by the XML specification for attribute values.

The <xsl:key> declaration and the matching XSLT key() function overcome these limitations, enabling you to locate source-tree nodes by matching either elements or attributes of any type.

When there are no obvious identifier values to use in the source data, you can use the XSLT generate-id() function to associate nodes with one another. In any single execution of a stylesheet, this function will always cause an identical unique value to be generated for a given node. This property can be used to associate related items and generate linking information.

In this chapter you will do the following:

  • Review some characteristics of the ID and IDREF datatypes used in XML documents to uniquely identify items and refer to them.

  • Learn how to use the XPath 2.0 id() function on ID attributes to locate source items, and appreciate its limitations.

  • Use <xsl:key> and the key() function to work with more complex cross-reference structures, and locate non-unique items for processing.

  • Apply the generate-id() function to index the lines of ...

Get Beginning XSLT and XPath: Transforming XML Documents and Data 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.