xpath:number returns false for "0"
numbers, xpath, xslt, xslt-1.0
Solution
It is taken from XPath test if node value is number Test the value against NaN:
<xsl:if test="string(number(myNode)) != 'NaN'">
<!-- myNode is a number -->
</xsl:if>
This is a shorter version:
<xsl:if test="number(myNode) = myNode">
<!-- myNode is a number -->
</xsl:if>
<xsl:if test="number(0)">
the expression number(0) is evaluated to the boolean false() -- because by definition
boolean(0) is false()
Problem
I have an `xsl:variable` that can be either the empty string or a number. So I evaluate it like this: ``` <xsl:if test="number($var)"><node att="{number($var)}" /></xsl:if> ``` This works if `var` is the empty string, but it has the same effect if `var` is `0`: From -2 to 2: ``` <node att="-2" /> <node att="-1" /> <node att="1" /> <node att="2" /> ``` Is this a bug? Is there a different version of the `number` function that also captures `0`? Do I really have to add `or $var = '0'` to my `test` statement?