O'Reilly logo

Beginning XSLT and XPath: Transforming XML Documents and Data by Ian Williams

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required