tags:

views:

4343

answers:

2

I am totally new to XSLT and can't work out where I am going wrong with the following code.

<xsl:variable name="var" select="boolean('false')"/>

<xsl:if test="$var'">variable is true</xsl:if>

It is always returning true when it is meant to be false. Can anyone help?

+4  A: 

The boolean() function you are using is indeed doing it's job. For using explicit true and false values you should use the following functions:

<xsl:variable name="var_false" select="false()"/>
<xsl:variable name="var_true" select="true()"/>

Just FYI, per the MSDN documentation, boolean() returns the following:

  • If the argument is a negative or positive number, it is converted to the Boolean value true.
  • If the argument is zero or an NaN value, it is converted to false.
  • If the argument is a non-empty node-set, it is converted to true. An empty node-set is converted to false.
  • If the argument is a non-empty string, it is converted to true. An empty string is converted to false.
  • If the argument is an object of a type other than the four basic types, it is converted to a Boolean in a way that is dependent on that type.
Yuval A
You should reformat the penultimate item of your list to stand out more, as it is the crucial one. "boolean('')" would produce false as well.
Tomalak
@Tomalak, see my answer. It is a little bit more detailed and quotes the true source: the W3C XPath Spec, rather than "MSDN".
Dimitre Novatchev
@Dimitre: I don't think MSDN deviates too much from the W3C spec in this regard. :) But pointing *there* is of course more correct.
Tomalak
+12  A: 

The value of the $var variable as defined in:

   <xsl:variable name="var" select="boolean('false')"/>

is

   true()

This is because in XPath "false" is an ordinary string, as opposed to false(), which is the constructor for the boolean value false()

The two boolean values in XPath are (note that they are constructed!):

   true() and false()

The detail of converting any value to boolean are spelled outin the XPath Spec.:

"The boolean function converts its argument to a boolean as follows:

  • a number is true if and only if it is neither positive or negative zero nor NaN

  • a node-set is true if and only if it is non-empty

  • a string is true if and only if its length is non-zero

  • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type "

In your case the string "false" is not the number 0 and has a positive length, so the rule in the 3rd bullet above is applied, yielding true().

Therefore, to define a variable in XSLT 1.0, whose value is false(), one needs to write the definition as the following:

   <xsl:variable name="vMyVar" select="false()"/>

or, if you don't exactly remember this, you could always write:

   <xsl:variable name="vMyVar" select="1 = 0"/>

(specify any expression that evaluates to false()) and the XSLT processor will do the work for you.

In XSLT 2.0 it is always better to explicitly specify the type of the variable:

   <xsl:variable name="vMyVar" as="xs:boolean" select="false()"/>

Dimitre Novatchev
+1 for the exhaustive explanation.
Tomalak
@Tomalak: Thanks, I do believe in the "one true source" :)
Dimitre Novatchev
thanks guys, great help!
sydlawrence