XSLT <xsl:choose>
❮XSLT要素リファレンス
定義と使用法
<xsl:choose>要素は、<xsl:when>および<xsl:otherwise>と組み合わせて使用され、複数の条件付きテストを表現します。
<xsl:when>がtrueでない場合、<xsl:otherwise>の内容が処理されます。
<xsl:when>がtrueでなく、<xsl:otherwise>要素が存在しない場合、何も作成されません。
ヒント:単純な条件付きテストの場合は、代わりに<xsl:if>要素を使用してください。
構文
<xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>
属性
なし
例
以下のコードは、CDの価格が10を超えると、アーティストの列にピンクの背景色を追加します。
例
<?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">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
「color」という名前の変数を宣言します。その値を現在の要素のcolor属性に設定します。現在の要素にcolor属性がない場合、「color」の値は「green」になります。
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
❮XSLT要素リファレンス