Branching Elements of XSLT
Three XSLT elements are used for branching: <xsl:if>, <xsl:choose>, and <xsl:for-each>. The first two are much like the if and case statements you may be familiar with from other languages, while the for-each element is significantly different from the for or do-while structures in other languages. We’ll discuss all of them here.
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, check out the <xsl:choose> element described in the next section.)
Notice that we used > instead of > in the attribute value. You’re always safe using > here, although some XSLT processors process the greater-than sign correctly if you use > instead. If you need to use the less-than operator (<), you’ll have to use the < entity. The same holds true for the less-than-or-equal operator (<=) and the greater-than-or-equal (>=) operators. See Section B.4.2 for more information on this topic.
Converting to boolean values
The <xsl:if> element is pretty simple, but it’s the first time ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access