@634

XSLT - キーの定義 (xsl:key)

Advertisement

書式

<xsl:key name="参照名" match="ノード指定" use="キー対象の指定"/>

<xsl:key name="num" match="book" use="@id"/>
この文を記述することにより、book ノードの属性 id を num という名前で参照することが可能になる。
参照方法 → key('参照名','値')
使い方は後述のサンプルを参照

サンプルコード

sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>

<document>
    <book id="001">
        <title>AAA</title>
        <price>4000</price>
    </book>
    <book id="002">
        <title>BBB</title>
        <price>1000</price>
    </book>
    <book id="003">
        <title>CCC</title>
        <price>2000</price>
    </book>
    <book id="004">
        <title>DDD</title>
        <price>1500</price>
    </book>
    <book id="005">
        <title>EEE</title>
        <price>500</price>
    </book>
</document>
style.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="num" match="book" use="@id" />

<xsl:template match="document">
    <html>
    <head>
    <title>XSLT</title>
    </head>
    <body>
    <xsl:apply-templates select="key('num','002')" />
    </body>
    </html>
</xsl:template>

<xsl:template match="key('num','002')">
<h1>結果</h1>
<xsl:value-of select="title"/><br />
<xsl:value-of select="price"/>
</xsl:template>

</xsl:stylesheet>
生成されるコード
<html>
<head>
<title>XSLT</title>
</head>
<body>
<h1>結果</h1>
BBB<br />
1000
</body>
</html>

Advertisement

ショートカット

634
634ブログ
このカテゴリのトップページに戻る
Incubator(Pukiwiki)
634ラボ
   UIコレクションギャラリー
   ZO-3ジェネレーター

サイト検索


Y!ログール

ビリヤード