Conditional Processing

Controlling choices in XSLT as to how and whether a node is to be processed falls into two categories:

  • Choice of two processing alternatives, one of which is to do nothing

  • Choice of multiple (greater than two) options

The xsl:if Element

The xsl:if element in XSLT corresponds broadly to if...then... else type statements in other programming languages, but in XSLT there is no else option. If you want an else option, you must use xsl:choose, described later in this chapter.

An xsl:if element is always nested within an xsl:template element. XSLT elements that are nested within templates are termed instructions or instruction elements.

The general form is like this:

 <xsl:template> <!-- Other content can go here. --> <xsl:if ...

Get Sams Teach Yourself XML in 10 Minutes 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.