XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Keys and identifiers / Runtime identifiers for elements – generate-id()
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
Function generate-id() assigns elements individual identifiers during stylesheet execution. It generates an XML name using a processor-specific algorithm to produce an ASCII string unique across nodes, even in different documents.
The identifier always begins with a letter, ensuring a valid XML name (NMToken). Otherwise, each application is free to choose its generated identifier format.
Identifiers are virtual, not stored directly. They help the processor remember an element. Imagine that the first call to function generate-id() for an element adds a property with the generated value. Subsequent calls only read it. The exact value cannot be controlled; only its stability during processing is guaranteed.
Automatic table of contents with generate-id()
One use of generate-id() is automatically generating ID-IDREF relationships for internal HTML links such as <a name="id"> and <a href="#id">.
An XML document <dokument> has several chapters <kapitel>, each with a title <titel> and text <text>:
<dokument>
<kapitel>
<titel>Identifier mit Verfallszeit</titel>
<text>Die Funktion generate-id() ermöglicht es, Elementen für den Zeitraum der XSLT-Verarbeitung (Laufzeit des Stylesheets) individuelle Identifier zuzuordnen. ...</text>
</kapitel>
...
</dokument>
Chapter titles generate a table of contents, also a list of jump links <a href="#..."> to the chapters marked with attribute id in the document body. The matching value for id is also generated using generate-id(). The function is thus called twice when processing element <title>:
The first call:
While constructing the table of contents, an attribute value template uses the identifier as a link’s href attribute value. The xsl:for-each (loop ) constructs a sequence of all <titel> elements, each becoming the current node. The function generate-id() then runs without an argument, referring automatically to the current element <titel> of the loop.
The second call:
Outputting the chapter calls the function again. Here, generate-id() takes a node test targeting the current chapter’s child <titel>. The generated identifier <titel> must match the earlier one and can serve as the id attribute value marking the link target.
<xsl:template match="/">
<html>
<head>
<title>XSLT 2.0 und XPath 2.0: generate-id()</title>
</head>
<body>
<h1>Inhaltsverzeichnis mit generate-id()</h1>
<ul>
<xsl:for-each select="//titel">
<li><a href="#{generate-id()}"><xsl:value-of select="."/></a></li>
</xsl:for-each>
</ul>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="kapitel">
<h3 id="{generate-id(titel)}"><xsl:value-of select="titel"/></h3>
<p><xsl:value-of select="text"/></p>
<p><a href="#">nach oben</a></p>
<hr width="60%" noshade="noshade" size="1"/>
</xsl:template>
Code example: kap03/3.10/generate-id.xsl (excerpt).
When and where virtual IDs are generated does not matter. Call order only affects result element order in the output.
These markers cannot be found during processing through function id(); they are not genuine IDs in that sense. Function id() finds only actual identifiers declared in the XML document, requiring a DTD or schema.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn