| <xsl:otherwise> | |
| <xsl:choose> 要素内の else または default の場合を定義します。この要素は、<xsl:choose> 要素内の最後に指定する必要があります。 | |
| カテゴリ | |
|
サブ命令 (<xsl:otherwise> は <xsl:choose> 要素の一部として指定する必要があります)。 |
|
| 必須の属性 | |
|
なし。 |
|
| 省略可能な属性 | |
|
なし。 |
|
| コンテンツ | |
|
テンプレート。 |
|
| 指定先 | |
|
<xsl:choose> 要素。 |
|
| 定義先 | |
|
XSLT 9.2 節「xsl:choose による条件処理」 |
|
| 例 | |
|
例として、HTML テーブル内のセルの背景色に対する一連の値を調べる <xsl:choose> 要素を使用します。この XML ドキュメントを入力として使用します。 <?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list> 次のスタイルシートでは、<xsl:attribute> 要素内で <xsl:choose> を使用して bgcolor 属性の正しい値を決定します。<xsl:otherwise> 要素により、ソースドキュメントの 4 つ目の <listitem> ごとに値 whitesmoke が生成されます。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select="list/title"/>
</title>
</head>
<body>
<h1><xsl:value-of select="list/title"/></h1>
<table border="1">
<xsl:for-each select="list/listitem">
<tr>
<td>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="@bgcolor">
<xsl:value-of select="@bgcolor"/>
</xsl:when>
<xsl:when test="position() mod 4 = 0">
<xsl:text>papayawhip</xsl:text>
</xsl:when>
<xsl:when test="position() mod 4 = 1">
<xsl:text>mintcream</xsl:text>
</xsl:when>
<xsl:when test="position() mod 4 = 2">
<xsl:text>lavender</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>whitesmoke</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
生成された HTML ドキュメントは次のとおりです。4 行ごとに背景色が whitesmoke に設定されています。この値は、<xsl:otherwise> 要素によって生成されます。 <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Albums I've bought recently:</title> </head> <body> <h1>Albums I've bought recently:</h1> <table border="1"> <tr> <td bgcolor="mintcream">The Sacred Art of Dub</td> </tr> <tr> <td bgcolor="lavender">Only the Poor Man Feel It</td> </tr> <tr> <td bgcolor="whitesmoke">Excitable Boy</td> </tr> <tr> <td bgcolor="papayawhip">Aki Special</td> </tr> <tr> <td bgcolor="mintcream">Combat Rock</td> </tr> <tr> <td bgcolor="lavender">Talking Timbuktu</td> </tr> <tr> <td bgcolor="whitesmoke">The Birth of the Cool</td> </tr> </table> </body> </html> 表示された HTML ドキュメントの外観は、図 A-7 のようになります。 さまざまな背景色を繰り返すドキュメント |
|