generate-id


generate-id

The generate-id() function generates a string, in the form of an XML Name , that uniquely identifies a node. The result is guaranteed to be different for every node that participates in a given transformation.

For example, the expression «generate-id(..) » might return the string «N015732 » when using one XSLT processor, and «b23a1c79 » when using another.

Changes in 2.0

None.

Signature

Argument

Data Type

Meaning

node (optional)

node() ?

The input node. If the argument is omitted, the context node is used. If an empty sequence is supplied, the zero-length string is returned.

Result

xs:string

A string value that uniquely identifies the node. This will consist only of ASCII alphanumeric characters , and the first character will be alphabetic. This makes the identifier suitable for use in many contexts, for example as an ID value in an XML document or an HTML anchor.

Effect

If the node argument is omitted, it defaults to «. » , the context item. A type error occurs if this is not a node.

If the node argument is an empty sequence, the function returns a zero-length string.

The function returns an arbitrary string. Within a given transformation, the function will always return the same string for the same node, and it will always return different strings for different nodes: in other words, «generate-id($A) = generate-id ($B) » is true if and only if «$A is $B » is true, and this includes the case where the nodes are in different documents.

The generated identifiers are unique within a single execution of the stylesheet. If the same stylesheet is used several times, with the same or different source documents, the function may generate the same identifiers in each run but is under no obligation to do so.

Usage and Examples

In XSLT 1.0, the generate-id() function was often used to determine whether two expressions represented the same node, that is, to compare nodes by identity. XPath 2.0 offers the «is » operator for this purpose, so this usage can be expected to dwindle.

The main intended purpose of the generate-id() function is to create links in the output document. For example, it can be used to generate ID and IDREF attributes in an output XML document, or <a name="X"> and <a href="#X"> pairs in an output HTML document.

Using generate-Id() to Create Links
start example

This example takes as input a file resorts.xml containing details of a collection of holiday resorts, each of which includes a list of hotels.

Source

  <resorts>   <resort>   <name>Amsterdam</name>   <details>A wordy description of Amsterdam</details>   <hotel>   <name>Grand Hotel</name>   <stars>5</stars>   <address> . . . </address>   </hotel>   <hotel>   <name>less Grand Hotel</name>   <stars>2</stars>   <address> . . . </address>   </hotel>   </resort>   <resort>   <name>Bruges</name>   <details>An eloquent description of Bruges</details>   <hotel>   <name>Central Hotel</name>   <stars>5</stars>   <address> . . . </address>   </hotel>   <hotel>   <name>Peripheral Hotel</name>   <stars>2</stars>   <address> . . . </address>   </hotel>   </resort>   </resorts>  

Stylesheet

The stylesheet resorts.xsl constructs an output HTML page in which the hotels are listed first, followed by information about the resorts. Each hotel entry contains a hyperlink to the relevant resort details. The links for the resorts are generated using generate-id() applied to the <resort> element.

This is a complete stylesheet that uses the Simplified Stylesheet syntax introduced on page 119, in Chapter 3.

  <html   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xsl:version="1.0"   >   <body>   <h1>Hotels<h1>   <xsl:for-each select="//hotel ">   <xsl:sort select= "number (stars)"   order= "descending" data-type="number"/>   <h2><xsl:value-of select="name"/></h2>   <p>Address: <xsl:value-of select="address"/></p>   <p>Stars: <xsl:value-of select="stars"/></p>   <p>Resort: <a href="#{generate-id(parent::resort)}">   <xsl:value-of select="parent::resort/name"/></a></p>   </xsl:for-each>   <h1>Resorts</h1>   <xsl:for-each select="//resort">   <h2><a name="{generate-id()} "> ,   <xsl:value-of select="name"/>   </a></h2>   <p><xsl:value-of select="details"/></p>   </xsl:for-each>   </body>   </html>  

Notice how generate-id() is used twice, once to generate the identifier of the resort, the next time to generate a link from the hotel.

Output

The following output was obtained using Saxon. I have added some extra indentation to show the structure. A different product will generate different identifiers in the <a> elements, but the links will work just as well.

  <html>   <body>   <h1>Hotels</h1>   <h2><Grand Hotel</h2>   <p>Address: . . . </p>   <p>Stars:5</p>   <p>Resort: <a href="#d0e3">Amsterdam</a></p>   <h2>Central Hotel</h2>   <p>Address: . . . </p>   <p>Stars: 5</p>   <p>Resort: <a href=#d0e36">Bruges</a></p>   <h2>Less Grand Hotel</h2>   <p>Address: . . . </p>   <p>Stars: 2</p>   <p>Resort:<a href=''#d0e3">Amsterdam</a></p>   <h2>Peripheral Hotel</h2>   <p>Address: . . . </p>   <p>Stars: 2</p>   <p>Resort: <a href="#d0e36">Bruges</a></p>   <h1>Resorts</h1>   <h2><a name="d0e3">Amsterdam</a></h2>   <p>A wordy description of Amsterdam</p>   <h2><a name="d0e36">Bruges</a></h2>   <p>An eloquent description of Bruges</p>   </body>   </html>  
end example
 

There is no inverse function to generate-id(): specifically , there is no direct way to find a node if its generated id is known, other than the very inefficient:

  //node() [generate-id()=$X]  

If you need to do this, however, you can set up a key definition as follows .

  <xsl:key name="gid-key" match="*" use="generate-id() "/>  

Then find the element with a given id value using the expression:

  key('gid-key', $X)  

It is important to appreciate that the generated identifiers bear no resemblance to any ID attribute values in the source document, so the nodes cannot be found using the id() function.

Also, the ID values generated in one run of the processor may be different from those generated in a subsequent run. You need to bear this in mind if you are using the ID values as hyperlinks . If the transformation is likely to be run more than once, then it isn't safe to reference these ID values from another document, or even to save them as a bookmark in a browser.

See Also

  • id() in XPath 2.0 Programmer's Reference .

  • key() on page 572.

  • «is » operator in XPath 2.0 Programmer's Reference .




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net