@634

XSLT - 変数の利用(xsl:variable)

Advertisement

書式

変数の宣言。
<xsl:variable name="変数名">値</xsl:variable>

<xsl:variable name="変数名" select="式" />

宣言した変数を使用する際は $ 記号を使用する。
$変数名

<!-- 変数宣言 -->
<xsl:variable name="title">タイトル:</xsl:variable>

<!-- 変数の使用 -->
<xsl:value-of select="$title" />

サンプルコード

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:variable name="title">タイトル:</xsl:variable>

<xsl:template match="title">
    <h1>
        <xsl:value-of select="$title"/>
        <xsl:value-of select="."/>
    </h1>
</xsl:template>

<xsl:template match="price">
    <p>
        <xsl:value-of select="."/>
    </p>
</xsl:template>

</xsl:stylesheet>
生成されるコード
<h1>タイトル:AAA</h1>
<p>4000</p>
<h1>タイトル:BBB</h1>
<p>1000</p>
<h1>タイトル:CCC</h1>
<p>2000</p>
<h1>タイトル:DDD</h1>
<p>1500</p>
<h1>タイトル:EEE</h1>
<p>500</p>

Advertisement

ショートカット

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

サイト検索


Y!ログール