Skip to content

XSLT generate-id() Function

How the XSLT generate-id() function creates unique identifiers for nodes, enabling precise linking and reference handling in XML transformations.

XSLT generate-id() Function

The generate-id function returns a string value that uniquely identifies a specified node.

If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter, it defaults to the current node.

Syntax

string generate-id(node-set?)

Input Parameters

Parameter Required/Optional Description
node-set Optional Specifies on which node-set to generate a unique id

Example

Consider the following example about a bookstore:

books.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Tips</title>
		<author>Tom Nolan</author>
		<year>2022</year>
		<price>39.99</price>
	</book>
	<book category="web">
		<title lang="en">Learn XML</title>
		<author>Tutorial Reference</author>
		<year>2022</year>
		<price>9.99</price>
	</book>
</bookstore>
books.xslxml
<?xml version="1.0"  encoding="UTF-8"?>  
<xsl:stylesheet version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  
<xsl:template match="/">  
	<html>  
		<body>  
			<h3>Authors:</h3>  
			<ul>  
				<xsl:for-each select="bookstore/book">  
				<li>  
					<a href="#{generate-id(author)}">  
					<xsl:value-of select="author"  /></a>  
				</li>  
				</xsl:for-each>  
			</ul>  
			<hr />  
			<xsl:for-each select="bookstore/book">  
				Author: <a id="{generate-id(author)}">  
				<xsl:value-of select="author"  /></a>  
				<br />  
				Title: <xsl:value-of select="title"  />  
				<br />  
				Price: <xsl:value-of select="price"  />  
				<hr />  
			</xsl:for-each>  
		</body>  
	</html>  
</xsl:template>  
  
</xsl:stylesheet>
Navigation