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!
Conditionals in XSLT are either an unary “if”:
or more like the switch statement of other languages:
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“falsefalse(so isNaN)falsefalse()isfalsetrue(most notably:'false'istrueand'0'istrue)Edit: There is of course a more advanced (and more idiomatic) method to control program flow, and that’s template matching:
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:
XSLT beginners tend to pick the latter form for its familiarity, but examining template matching instead of using conditionals is worthwhile. (also see.)