Reputation: 6485
I working on a site which some if/or statements in XSL and being a little unfamilar with the language i'm not certain how to accomplish:
if [condion one is met] or [condition two is met] then do [action] otherwise do [alternative action]
can anyone offer some examples?
Thanks in advance!
Upvotes: 21
Views: 57360
Reputation: 338208
Conditionals in XSLT are either an unary "if":
<xsl:if test="some Boolean condition">
<!-- "if" stuff (there is no "else" here) -->
</xsl:if>
or more like the switch statement of other languages:
<xsl:choose>
<xsl:when test="some Boolean condition">
<!-- "if" stuff -->
</xsl:when>
<xsl:otherwise>
<!-- "else" stuff -->
</xsl:otherwise>
</xsl:choose>
where there is room for as many <xsl:when>
s as you like.
Every XPath expression can be evaluated as a Boolean according to a set of rules. These (for the most part) boil down to "if there is something -> true
" / "if there is nothing -> false
"
false
false
(so is NaN
)false
false()
is false
true
(most notably: 'false'
is true
and '0'
is true
)Edit: There is of course a more advanced (and more idiomatic) method to control program flow, and that's template matching:
<xsl:template match="node[contains(., 'some text')]">
<!-- output X -->
</xsl:template>
<xsl:template match="node[not(contains(., 'some text'))]">
<!-- output Y -->
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select=".//node" />
</xsl:template>
Writing templates that match specific nodes and using <xsl:apply-templates>
to make the XSLT processor choose the appropriate ones is superior to writing complex <xsl:if>
or <xsl:choose>
constructs.
The above sample is equivalent to the imperative style:
<xsl:template match="/">
<xsl:for-each select=".//node">
<xsl:choose>
<xsl:when test="contains(., 'some text')">
<!-- output X -->
</xsl:when>
<xsl:when test="not(contains(., 'some text'))">
<!-- output Y -->
</xsl:when>
<xsl:choose>
<xsl:for-each>
</xsl:template>
XSLT beginners tend to pick the latter form for its familiarity, but examining template matching instead of using conditionals is worthwhile. (also see.)
Upvotes: 44
Reputation: 3685
XSL has an <xsl:if>
, but you're probably looking more for a <xsl:choose>
/ <xsl:when>
/ <xsl:otherwise>
sequence. Some examples here (near the bottom). Maybe:
<xsl:choose>
<xsl:when test="[conditionOne] or [conditionTwo]">
<!-- do [action] -->
</xsl:when>
<xsl:otherwise>
<!-- do [alternative action] -->
</xsl:otherwise>
</xsl:choose>
Upvotes: 4
Reputation: 847
The general if statement syntax is
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
Not sure if XSL has the else condition but you should be able to test if true then test if false or the other way around.
Upvotes: 1
Reputation: 8481
In this case you would have to use a xsl:choose
. It's like using if/else with a final else.
<xsl:choose>
<xsl:when test="condition one or condition two">
<!-- action -->
</xsl:when>
<xsl:otherwise>
<!-- alternative action -->
</xsl:otherwise>
</xsl:choose>
Upvotes: 0