0
<xsl:if test="(1 &gt; 2)">
</xsl:if>

Is there anything shorter? Tried (false) and ('false') but with no luck.

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212

3 Answers3

5

You were almost there. This is the correct way to use a boolean:

<xsl:when test="false()">

It is slightly shorter than your solution, but I would not say that brevity matters here. Rather, using booleans for truth tests is a cleaner solution than taking advantage of the "side effects" of number comparisons.

See this answer for more info on how to create booleans in XSLT.

To illustrate this, assume the following

XML input

<?xml version="1.0" encoding="utf-8"?>
<root/>

Stylesheet

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:template match="/root">
      <xsl:choose>
         <xsl:when test="false()">
            <xsl:copy/>
         </xsl:when>
         <xsl:otherwise/>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>

If you change false() to true(), the root element is output.

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • Thank you. It is really hard to choose the best answer among your and Matt Jones answers. Still thinking for several more hours :) Let's people vote and I will choose with a higher rating. Thank you. – Haradzieniec Jan 30 '14 at 13:58
4

Mathias Müller's answer is correct and informative, but from a purely pedantic point of view (and you did ask for the shortest false evaluating xsl:if statement!) you can do this:

<xsl:if test="1=0">

It will always evaluate to false, although I'm not really sure what the point of this would be...

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
  • +1 because you do point out that it's the pedantically correct answer, hence addressing the reasonable objections one might have. – Jon Hanna Jan 30 '14 at 11:02
  • 1
    I suppose that (still pedantically speaking!) the shortest possible false evaulating xsl:if statement would be no statement at all! But I've given +1 to @MathiasMüller because his answer is the most informative and useful. – Matt Hogan-Jones Jan 30 '14 at 11:07
  • The reason was to add "false" in front of if-condition1-and-condition2 so it will always be false. Kind of commenting the code in one place. – Haradzieniec Jan 30 '14 at 13:56
3

Shortest in what terms? The string-length of the expression? Try:

<xsl:if test="''">

Or the number of characters you must type? Try:

<xsl:if test="0"> 

Or did you mean the number of evaluations required - which IMHO is the only legitimate question to ask here. If that is the case, then the answer given by Mathias Müller is the one and only correct answer, because the expression used is already a Boolean. Otherwise this would be a question that should appear in a trivia contest, not here.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Yep, `test="0"` is as short as it gets! Likewise, you can use `test="1"` for a condition that is always treated as true. – JLRishe Jan 30 '14 at 14:57