XSLT <xsl:when>要素
完全な XSLT 要素リファレンスマニュアル
定義と使用方法
<xsl:when> 要素は、<xsl:choose> 要素の関連アクションを指定するために使用します。 <xsl:when> 要素は式を評価し、true を返す場合、指定されたアクションを実行します。
注意:<xsl:when> 要素は <xsl:choose> 要素および <xsl:otherwise> 要素と連携して、複数の条件テストを表現します。
構文
<xsl:when
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
属性
| 属性 | 值 | 説明 |
|---|---|---|
| test | boolean-expression | 必須。テストするブール式を指定します。 |
例 1
次のコードは、cd の価格が 10 より高い場合に、artist 列にピンクの背景色を追加します。
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
<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>
、 、 。
例 2
"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>
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
完全な XSLT 要素リファレンスマニュアル その他の拡張