Reputation: 1409
Recently, I wrote some personal programs using XSLT. I am surprised that XPATH has 'if then else' statement, no 'else if'.
For example, I can only use:
if *** then ***
else ***
but I can't use:
if *** then ***
else if *** then ***
else ***
Does XPath support else-if statement? The only way to simulate else-if is nesting if else?
Upvotes: 1
Views: 4931
Reputation: 163322
An expression such as
if (x=3)
then 1
else if (x=4)
then 5
else 6
is perfectly legal in XPath 2.0.
Upvotes: 8
Reputation: 56162
You can use xsl:choose
The xsl:choose element selects one among a number of possible alternatives. It consists of a sequence of one or more
xsl:when
elements followed by an optionalxsl:otherwise
element. Eachxsl:when
element has a single attribute, test, which specifies an expression. The content of thexsl:when
andxsl:otherwise
elements is a sequence constructor.When an xsl:choose element is processed, each of the
xsl:when
elements is tested in turn (that is, in the order that the elements appear in the stylesheet), until one of thexsl:when
elements is satisfied. If none of thexsl:when
elements is satisfied, then the xsl:otherwise element is considered, as described below.
<xsl:choose>
<xsl:when test="expression1">
... some output ...
</xsl:when>
<xsl:when test="expression2">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
Upvotes: 0