Re: [xsl] questions on "number('+5')"

Subject: Re: [xsl] questions on "number('+5')"
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Mon, 30 Aug 2010 18:43:35 +0100
So "number('+5')" should result in NaN.

In XSLT 1.0, yes, it should. In XSLT 2.0 it should result in the integer 5.
Here is the result of "number('+5')" for different XSLT processors:
Chrome      NaN
DataPower   NaN
Firefox     NaN
IE          NaN
Opera       5
Safari      NaN
saxon       5
xalan       5
xsltproc    NaN

Saxon 9.x correctly reports the result 5. Saxon 6.5.5 correctly reports the result NaN.



Two questions: 1) Is it true that XSLT processors returning 5 are non-compliant?
They are non-compliant with XSLT 1.0, yes.

2) Is the following a good workaround?

<xsl:variable name="str" select="'+5'"/>
<xsl:value-of select="number(substring($str,1+starts-with($node,'+')))"/>

It depends a bit on the syntax you want to accept and the error checking you want to perform. For many purposes, number(translate($str, '+','')) would work adequately.

Michael Kay
Saxonica

Current Thread