|| in boolean expression
Added by Vladimir Nesterovsky over 6 years ago
Recently I've run into simple bug on my side in xslt. It goes like this:
<xsl:if test="a || b">
...
</xsl:if>
It's not always easy to switch the mind between syntaxes, so some C-like artefact has crept into xpath syntax.
Correct expression was: "a or b".
It took time to figure out the problem, because there was no any compile- or run- time manifestation.
I would like to suggest, if it's easy, to issue info message in this case. Because my intention was clearly logical or, and not concatenation.
Replies (4)
Please register to reply
RE: || in boolean expression - Added by Michael Kay over 6 years ago
Are $a and $b declared as xs:boolean? I guess if we know that then an advisory warning is possible - as you say, no-one is likely to concatenate two booleans. It's not possible with other expressions like @a || @b -- that's a perfectly sensible concatenation operation, and if you intended a boolean "or", there's not much we can do about it.
I don't recall why string concatenation does strong casting of its operands to strings, unlike other functions and operators. Presumably it's because of expectations set by other languages.
RE: || in boolean expression - Added by Vladimir Nesterovsky over 6 years ago
In my case both variables were declared as xs:boolean, so expression turned to 'falsefalse', with effective boolean value true().
RE: || in boolean expression - Added by Michael Kay over 6 years ago
I've added the warning for the next release - if all arguments of a call to concat() are statically known to be singleton booleans. (a || b gets turned into a call on concat() long before type-checking can happen, so we can't distinguish then between a||b
and concat(a, b)
).
Please register to reply