tags:

views:

1426

answers:

4

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!

A: 

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>
Zack Mulgrew
A: 

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>
pianoman
+5  A: 

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"

  • the empty string is false
  • 0 is false (so is NaN)
  • the empty node set is false
  • the result of false() is false
  • every other literal value is true (most notably: 'false' is true)
  • the result of expressions is evaluated with said rules (no surprise here)
Tomalak
If I understand you correctly I should be applying a dual test in boolean <xsl:if test="string-length($name) > 0 or string-length($nameTwo) > 0"> or should I be using multiple when/otherwise sets?
toomanyairmiles
It depends. If you wanted to check if *any* of the strings is not empty, you could also do: <xsl:if test="concat($name, $nameTwo) != ''">
Tomalak
A: 

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.

Danny
You're right about the `else`; XSL has `<xsl:otherwise>` instead.
pianoman