共用方式為


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 資料型別參考