Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional statements in xsl

Tags:

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!

like image 570
toomanyairmiles Avatar asked Jul 21 '09 15:07

toomanyairmiles


People also ask

How do you use conditional in XSLT?

The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

How do you add an if else condition in XSLT?

XSL specifications implies a function<xsl: choose> to implement the functionality of if-else statements. To work with multiple options, we are supposed to use <choose> along with <otherwise> which is equivalent to anything else. The attribute is evaluated until the processor finds the condition that is met true.

How do you include conditional statements in XML?

Elements/attributes can be conditionally includes/excluded from the translation process using ezParse. There are two types of conditional statements supported by ezParse; (1) a condition to specify source text within a rule and (2) a condition to specify target text in a rule.

How do you write a while loop in XSLT?

You can format your XSLT stylesheet to go to a specific node, and then loop through the given node set. You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through.


Video Answer


2 Answers

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>
like image 37
Daniel F. Thornton Avatar answered Oct 02 '22 05:10

Daniel F. Thornton


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 and '0' is true)
  • the result of expressions is evaluated with said rules (no surprise here)

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.)

like image 200
Tomalak Avatar answered Oct 02 '22 04:10

Tomalak