The <xsl:if> Element

The <xsl:if> element looks like this:

<xsl:if test="count(zone) > 2">
  <xsl:text>Applicable zones: </xsl:text>
  <xsl:apply-templates select="zone"/>
</xsl:if>

The <xsl:if> element, surprisingly enough, implements an if statement. The element has only one attribute: test. If the value of test evaluates to the boolean value true, then all elements inside the <xsl:if> are processed. If test evaluates to false, then the contents of the <xsl:if> element are ignored. (If you want to implement an if-then-else statement, see the section The <xsl:choose> Element” later in this chapter.)

Notice that we used the character > in the value of the test attribute. If you need to use the less-than operator (<), you’ll have to use the &lt; entity. The same holds true for the less-than-or-equal operator (<=).

Converting to boolean values

The <xsl:if> element is pretty simple, but it’s the first time we’ve had to deal with boolean values. These values will come up later, so we might as well discuss them here. Attributes such as the test attribute of the <xsl:if> element convert whatever their values happen to be into a boolean value. If that boolean value is true, the <xsl:if> element is processed. (The <xsl:when> element, which we’ll discuss in the section The <xsl:choose> Element” later in this chapter, has a test attribute as well.)

[1.0] Here’s the rundown of how various datatypes are converted to boolean values:

number

If a number is positive or negative zero, it is false. If a numeric ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.