generate-id 函数

返回唯一标识 node-set 参数中按文档顺序的第一个节点的字符串。

string generate-id(node-set?)

注释

唯一标识符必须由 ASCII 字母数字字符组成,必须以字母字符开头。因此,字符串在语法上是 XML 名称。不保证生成的唯一标识符可以与源文档中指定的任何唯一 ID 截然不同。如果 node-set 参数为空,则返回空字符串。如果省略该参数,将默认为上下文节点。

示例

XML 文件 (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 文件 (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>

以下是格式化输出:

generate_id 函数的格式化输出

如果在 Internet Explorer 中单击鼠标左键,将出现一个警告框,显示“IDAHAGJD”。

如果在 Internet Explorer 中单击鼠标右键,将出现一个警告框,显示“IDAPAGJD”。

请注意,ID 值在运行时生成。因此,特定的值在不同的转换调用中会有所变化。

以下是处理器输出:

<html>

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

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

</html>

请参阅

参考

XML 数据类型引用