XSLT <xsl:key>
❮完全なXSLT要素リファレンス
定義と使用法
<xsl:key>要素は、key()関数を使用してスタイルシートで使用できる名前付きキーを宣言する最上位の要素です。
注:キーは一意である必要はありません!
構文
<xsl:key
name="name"
match="pattern"
use="expression"/>
属性
Attribute | Value | Description |
---|---|---|
name | name | Required. Specifies the name of the key |
match | pattern | Required. Defines the nodes to which the key will be applied |
use | expression | Required. The value of the key for each of the nodes |
例1
「persons.xml」というXMLファイルがあるとします。
<persons>
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</persons>
次のように、XSLファイルでキーを定義できます。
<xsl:key name="preg" match="person" use="@id"/>
id = "050676"の人を見つけるには、(XSLファイルに)次のように記述します。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="preg" match="person" use="@id"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('preg','050676')">
<p>
Id: <xsl:value-of select="@id"/><br />
Name: <xsl:value-of select="@name"/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
❮完全なXSLT要素リファレンス