<xsl:if>  
if ステートメントを実装します。test 属性と XSLT テンプレートを含みます。test 属性によりブール値が true と評価されると、XSLT テンプレートが処理されます。この要素は、if ステートメントのみを実装します。if-then-else ステートメントが必要な場合は、<xsl:choose> 要素を単独の <xsl:when><xsl:otherwise> を共に使用してください。
 
カテゴリ

命令

 
必須の属性
test
test 属性にはブール式を指定します。ブール値が true と評価された場合は、 < xsl:if > 要素内の XSLT テンプレートが処理されます。

 
省略可能な属性

なし。

 
コンテンツ

XSLT テンプレート。

 
指定先

<xsl:if> テンプレート内に指定します。

 
定義先

XSLT 9.1 節「xsl:if による条件処理」

 

次のスタイルシートに <xsl:if> 要素を示します。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Here are the odd-numbered items from the list:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="list/listitem">
      <xsl:if test="(position() mod 2) = 1">
        <xsl:number format="1. "/>
        <xsl:value-of select="."/>
        <xsl:value-of select="$newline"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>

このスタイルシートは <xsl:if> 要素を使用して、指定した <listitem> の位置が奇数であるかどうかを確認します。奇数の場合は、リスト項目が結果ツリーに書き込まれます。この XML ドキュメントに対してスタイルシートをテストします。

<?xml version="1.0"?>
<list>
  <title>A few of my favorite albums</title>
  <listitem>A Love Supreme</listitem>
  <listitem>Beat Crazy</listitem>
  <listitem>Here Come the Warm Jets</listitem>
  <listitem>Kind of Blue</listitem>
  <listitem>London Calling</listitem>
  <listitem>Remain in Light</listitem>
  <listitem>The Joshua Tree</listitem>
  <listitem>The Indestructible Beat of Soweto</listitem>
</list>

この変換を実行した場合の結果は次のとおりです。

Here are the odd-numbered items from the list:
1. A Love Supreme
3. Here Come the Warm Jets
5. London Calling
7. The Joshua Tree