GENERATING UNIQUE IDS AND LINKING TOTHM
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 Attribute.
Version = "1.0"> xsl: attribute> XSL: COPY> xsl: template> XSL: COPY> xsl: template> xsl: stylesheet> The Stylesheet Turns this XML Document sect2> sect1> chapter> INTO this one: sect2> sect1> chapter> 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 int in html link to the img element in the body of the document That Has the Illustration: Version = "1.0">
Title Linking to The Picture In The Poem Below. -> Pictures: b> xsl: for-energy> body> html> xsl: template> xsl: template>
xsl: template>
Rule Already Added It To Result Tree. ->
xsl: stylesheet>
With The Source Document Above, this Stylesheet Creates The Following HTML Document:
Pictures: b>
Then with expanded Wings He Steers His Flight P>
"Incumbent on the dusky air" b>
center>
aloft, incumbent on the dusky air p>
That Felt Unusual Weight, Till On Dry Land P>
"He Lights" b>
center>
he lights, if it were land this EVER BURNED P>
with solid, as the lake with liquid fire p>
"The Lake with Liquid Fire" b>
center>
body>
html>
(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 Twice:
As the xsl: for-each instruction in the "chapter" template rule adds each figure element's title to the result tree for the "Pictures:" list at the beginning of the result document, it puts each of these title elements inside of an HTML a element to link to the appropriate picture in the main part of the document. Each of these a elements has an href attribute to indicate the link destination. An href attribute that begins with a pound sign ( "#") looks for the link destination In The Same Document - Specifically, IT Looks for Another A ELEMENT WITH A Name Attribute Value Equal To The Part After The Pound Sign, An A Start-Tag of links to an a element with an start-tag elsewhere in the same document. Instead of the string "a123" identifying each link destination, this stylesheet uses the generate-id () function to make up an Identifying string. Because The graphic element node is passed to it as an argument, the function cre ates an ID string for each of the three graphic elements:. "N134691840", "N134692416", and "N134757920" To create the link destinations, the "graphic" template rule puts each HTML img element in the result tree inside of an a element. These img elements use the value of the source tree graphic elements' fileref attributes as their src value, and the a elements use the generate-id () function to create the values for their name attributes. Passing this function an argument of " "
is the same as passing it self :: node (), which in this case means passing it the graphic element node, so the XSLT processor generates an ID value for each graphic node. These are the same three nodes that the earlier use of the generate-id () created IDs for, and it creates the same three values:. "N134691840", "N134692416", and "N134757920" When this HTML file is displayed in a browser, each link in the opening "Pictures:" list will now go to the corresponding picture in the document.This consistency in the generate-id () function's treatment of a particular node, even if the function generates an ID for that node more than once, is the key to its power. These graphic .