... ALWAYS returns true? Since a bool...">

Test = "" in boolean always returns true

Why

<xsl:if test="<XPATH to boolean value here>"> ... </xsl:if> 

ALWAYS returns true?

Since a boolean value can be 0, "false" and "true" by definition, ONLY to check the boolean value is to compare the strings with them. It may not be right.

+6
xml xpath xslt boolean
source share
3 answers

test test works as if it were calling the boolean function. This feature does not work as you think. If its argument evaluates to node-set (which will be if you use the path as its argument), it will return true if node-set is not empty, and false otherwise. So effectively, you check for the existence of an element, not its value. If foo contains false ,

 <xsl:if test="/path/to/foo"> 

will always evaluate to true, since what you are really asking in this test is "does this element exist?" and not "is the value of the element true?" And the element exists.

The rule that booleans must be true , false , 1 or 0 is part of the XML schema ( which they see ), not XPath, which knows nothing about this rule. (XPath 1.0, that is, XPath 2.0 / XQuery 1.0 has a function fn:boolean that can, that is, XML Schema, evaluate boolean values.) To determine if a value is true in XSLT, you must explicitly check it:

 <xsl:if test="/path/to/foo = 'true' or /path/to/foo = '1'"> 
+8
source share

Here you can find a whole bunch of ideas:

Empty item

How to test an empty element in XSL?

0
source share

The xpath specification gives a boolean function. Thus, boolean (Value) will always return true or false.

0
source share

All Articles