@634

XSLT - 属性の作成 (xsl:attribute)

Advertisement

書式

<xsl:attribute name="属性名">
    属性値
</xsl:attribute>

<data>
    <xsl:attribute name="id">
        <xsl:value-of select="id"/>
    </xsl:attribute>
    <xsl:value-of select="title"/>
</data>
上記の場合 data という要素に id という属性が付加される。
<data price="対象ノードの id の値">title の値</data>

サンプルコード

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

<document>
    <book id="10">
        <title>AAA</title>
        <price>4000</price>
    </book>
    <book id="20">
        <title>BBB</title>
        <price>1000</price>
    </book>
    <book id="30">
        <title>CCC</title>
        <price>2000</price>
    </book>
    <book id="40">
        <title>DDD</title>
        <price>1500</price>
    </book>
    <book id="50">
        <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:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="document">
    <html>
    <head>
    <title>XSLT</title>
    </head>
    <body>
    <ul>
    <xsl:apply-templates/>
    </ul>
    </body>
    </html>
</xsl:template>

<xsl:template match="book">
    <li>
        <xsl:attribute name="price">
           <xsl:value-of select="price"/>
        </xsl:attribute>
        <xsl:value-of select="title"/>
    </li>
</xsl:template>

</xsl:stylesheet>
生成されるコード
    <html>
    <head>
    <title>XSLT</title>
    </head>
    <body>
    <ul>
    <li price="4000">AAA</li>
    <li price="1000">BBB</li> 
    <li price="2000">CCC</li>
    <li price="1500">DDD</li> 
    <li price="500">EEE</li>
    </ul>
    </body>
    </html>

Advertisement

ショートカット

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

サイト検索


Y!ログール

ビリヤード