web analytics

XSLT generate-id() Function

Options

codeling 1602 - 6666
@2016-03-03 22:04:01

In XSLT, 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?)

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>


The output:

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>

@2016-03-03 22:06:00

The generate-id() function will always return the same ID for the same node and different IDs for different nodes. Thus, comparing the generate-id values of two nodes is the way you check whether they are the same node as opposed to just two nodes that happen to have the same value.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com