XSLT <xsl:otherwise> 要素
定義と使用法
<xsl:otherwise> 要素は <xsl:choose> 要素のデフォルトの動作を定義します。条件 <xsl:when> が適用されない場合に発生します。
構文
<xsl:otherwise> <!-- Content:template --> </xsl:otherwise>
属性
なし
例
例 1
以下のコードは、cdの価格が30以上の場合に、artist列にピンクの背景色を追加し、それ以外の場合はartistのnameのみ出力します:
<?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>
XML ファイルを確認、XSL ファイルを確認、結果を確認。
例 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>