XSLT - 条件判定 (xsl:if)

Advertisement

書式

<xsl:if test="条件"/>
条件に一致したノードのみ処理する。
比較演算子を使用する場合、実体参照文字参照を使用する。

<xsl:if test="price&lt;=1000"/>
price の値が 1000 以下の場合、処理を行う。

サンプルコード

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="document">
    <html>
    <head>
    <title>XSLT</title>
    </head>
    <body>
    <table>
    <xsl:apply-templates/>
    </table>
    </body>
    </html>
</xsl:template>

<xsl:template match="book">
    <xsl:if test="price&lt;2000">
        <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="price"/></td>
        </tr>
    </xsl:if>
</xsl:template>


</xsl:stylesheet>
生成されるコード
<html>
<head>
<title>XSLT</title>
</head>
<body>
<table>
<tr><td>BBB</td><td>1000</td></tr>
<tr><td>DDD</td><td>1500</td></tr>
<tr><td>EEE</td><td>500</td></tr>
</table>
</body>
</html>

Advertisement

ショートカット

634トップページ
このカテゴリのトップページに戻る
634ラボ

サイト検索

Google

Web サイト内

Y!ログール