XSLT <xsl:when> 要素

定義および用法

<xsl:when> 要素は、<xsl:choose> 要素に対する関連アクションを定義するために使用されます。

<xsl:when> 要素は、式を計算し、true が返された場合に指定されたアクションを実行します。

注釈:<xsl:when> 要素は、<xsl:choose> 要素および <xsl:otherwise> 要素に関連する複数の条件テストを提供します。

構文

<xsl:when test="boolean-expression">
  <!-- Content: template -->
</xsl:when>

属性

属性 説明
テスト boolean-expression 必須。テストする布尔値の表达式を指定します。

例1

以下のコードは,cdのpriceが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>

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>