XSLT <xsl:if>
❮XSLT要素リファレンス
定義と使用法
<xsl:if>要素には、指定された条件が真の場合にのみ適用されるテンプレートが含まれています。
ヒント: <xsl:choose>を<xsl:when>および<xsl:otherwise>と組み合わせて使用して、複数の条件付きテストを表現してください。
構文
<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>
属性
Attribute | Value | Description |
---|---|---|
test | expression | Required. Specifies the condition to be tested |
例
CDの価格が10を超える場合は、タイトルとアーティストの値を選択します。
例1
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price > 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
各CDのタイトルを表示します。最後のCDではない場合、または最後のCDではない場合は、各CDタイトルの間に「、」を挿入します。最後のCDの場合は、「!」を追加します。タイトルの後ろ。それが最後の1枚のCDである場合は、タイトルの後ろに「」と「」を追加します。
例2
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<p>Titles:
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-1">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
❮XSLT要素リファレンス