When an XSLT stylesheet converts one XML document into another, the ability to add unique ID values to elements in the result document can make the result document much more useful to applications that use it. Adding unique IDs can, for example, turn each element into the unique target of a link.
XSLT's generate-id() function generates a unique ID for a node passed to it as an argument. This ID starts with a letter so that you can use it as the value of an XML ID attribute. For example, the following stylesheet copies an XML document and adds a uid ("unique ID") attribute to each chapter, sect1, and sect2 element. The xsl:value-of instruction uses the generate-id() function in the stylesheet's first template rule to create a value for these attributes.
The stylesheet turns this XML document
Then with expanded wings he steers his flight
"Incumbent on the Dusky Air"
Aloft, incumbent on the dusky Air
That felt unusual weight, till on dry Land
"He Lights"
He lights, if it were Land that ever burned
With solid, as the Lake with liquid fire
"The Lake with Liquid Fire"
into this one:
uid="N134711680">
Then with expanded wings he steers his flight
"Incumbent on the Dusky Air"
Aloft, incumbent on the dusky Air
uid="N134683456">
That felt unusual weight, till on dry Land
"He Lights"
He lights, if it were Land that ever burned
uid="N134684064">
With solid, as the Lake with liquid fire
"The Lake with Liquid Fire"
Your XSLT processor may generate different values with the generate-id() function. In fact, if you run the same stylesheet with the same input document a second time, the XSLT processor may not generate the same ID values that it generated the first time. However, if you call generate-id() more than once in one run with the same node as an argument, it generates the same ID value each time for that node. Because unique IDs are popular ways to identify link destinations, this consistency of the generate-id() function makes it a great way to generate links.
For example, adding a list of all of its illustrations at the beginning of the result document. If we make the result tree version an HTML file, we can use the generate-id function to turn each entry of this opening illustration list into an HTML link to the img element in the body of the document that has the illustration:
Pictures:
With the source document above, this stylesheet creates the following HTML document:
Pictures: href="#N134691840">"Incumbent on the Dusky Air"
href="#N134692416">"He Lights"
href="#N134757920">"The Lake with Liquid Fire"
Then with expanded wings he steers his flight
name="N134691840">
"Incumbent on the Dusky Air"
Aloft, incumbent on the dusky Air
That felt unusual weight, till on dry Land
name="N134692416">
"He Lights"
He lights, if it were Land that ever burned
With solid, as the Lake with liquid fire
name="N134757920">
"The Lake with Liquid Fire"
(To view the HTML document, you'll need to supply your own pic1.jpg, pic2.jpg, and pic3.jpg files.) The stylesheet uses the generate-id() ID twice: