generate-id Function

Returns a string that uniquely identifies the node in the node-set argument that is first in document order.

string generate-id(node-set?)

Remarks

The unique identifier must consist of ASCII alphanumeric characters and must start with an alphabetic character. Thus, the string is syntactically an XML name. There is no guarantee that a generated unique identifier will be distinct from any unique IDs specified in the source document. If the node-set argument is empty, the empty string is returned. If the argument is omitted, it defaults to the context node.

Example

XML File (data.xml)

<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>        
        <description>An in-depth look at creating applications with XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>A former architect battles corporate zombies.</description>
    </book>
</catalog>

XSLT File (sample.xsl)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
  <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="//book">
                    <button id="{generate-id(author)}" onclick="alert(this.id)">
                        <xsl:value-of select="author"/>
                    </button>
                </xsl:for-each>
            </body>
        </html>
  </xsl:template>
</xsl:stylesheet>

This is the formatted output:

Formatted output for generate_id function

When you click the left button in Internet Explorer, an alert box displays "IDAHAGJD".

When you click the right button in Internet Explorer, an alert box displays "IDAPAGJD".

Notice that the ID value is generate at run-time. Therefore, the specific values may vary in different invocation of the transformation.

This is the processor output:

<html>

<body><button id="IDAHAGJD" onclick="alert(this.id)">Gambardella, Matthew</button>

<button id="IDAPAGJD" onclick="alert(this.id)">Ralls, Kim</button></body>

</html>

See Also

Reference

XML Data Types Reference