次の方法で共有


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 値は実行時に生成されることに注意してください。そのため、変換を実行するたびに特定の 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 データ型リファレンス