XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / if

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The instruction xsl:if defines a condition deciding whether its template block is evaluated or skipped.

Classification

Instruction

Function group

Conditional processing

Introduced

XSLT 1.0

Position in the stylesheet and permitted contents:

The instruction xsl:if may occur anywhere in template blocks xsl:template. Multiple xsl:if may be nested.

Instruction xsl:if contains a block of arbitrary XSLT instructions, Literal Result Elements and text, instantiated when the condition holds.

Attributes:

Standard attributes apply. Instruction xsl:if also requires attribute test.

test

Value

xpath-expression

Usage

Required

Introduced

XSLT 2.0

The test attribute contains an XPath expression evaluated to a Boolean. If it yields true, the block runs; if it yields false, the block is skipped.

Purpose:

The instruction xsl:if conditionally runs or skips instructions using an XPath expression evaluated to a Boolean. Subexpressions can each yield a Boolean and be combined with operators and and or and individually negated with XPath function fn:not().

Combined with XSLT functions element-available() or function-available(), it can shield unsupported extension instructions or functions from processors unable to interpret them.

Warning – No ELSE branch for xsl:if conditions.
The xsl:if instruction resembles IF in other languages. But XSLT has no instruction linked to a preceding xsl:if, such as xsl:else, corresponding directly to ELSE or ELSE IF. Alternatives require xsl:choose with nested xsl:when and xsl:otherwise instructions.

Examples:

Example 1 – Test an attribute value:

<xsl:if test="@pruefwert='vorgabewert'">
  <p>Der Vorgabewert wird eingehalten.</p>
</xsl:if>

If the current node has attribute pruefwert with the specified value, output the message.

Example 2 – Test for a child element:

<xsl:if test="fn:not(telefonnummer)">
  <p>Keine Telefonnummer angegeben!</p>
</xsl:if>
  

If the current node has no child <telefonnummer>, output the message.

Example 3 – Test an extension instruction:

<xsl:if test="element-available('erw:superspezial')">
  <!-- Verwendung der Erweiterungsinstruktion -->
  <erw:superspezial />
</xsl:if>

The extension runs only if the XSLT processor recognized it while testing xsl:if; otherwise, the block is skipped.

Example 4 – Output book titles:

<xsl:for-each select="buchhandel/buch">
  <xsl:value-of select="titel"/>
  <xsl:if test="fn:position()!=fn:last()">
    <xsl:text>, </xsl:text>
  </xsl:if>
  <xsl:if test="fn:position()=fn:last()-1">
    <xsl:text> und </xsl:text>
  </xsl:if>
  <xsl:if test="fn:position()=fn:last()">
    <xsl:text>.</xsl:text>
  </xsl:if>
</xsl:for-each>

When listing titles, xsl:if and function fn:last() decide whether to output a comma, “und” or the final period after a title.

Element definition:

XSLT 1:

<!-- Category: instruction -->
<xsl:if 
     test = boolean-expression>

     <!-- Content: template -->
</xsl:if>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:if
     test = expression>

     <!-- Content: sequence-constructor -->
</xsl:if>

   

  

back to top