[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] [XSL] How to test "numericity" ?


Subject: [xsl] [XSL] How to test "numericity" ?
From: Alain <alainb06@xxxxxxx>
Date: Sun, 23 Mar 2008 18:33:18 +0100

Hi,

I want to test if a string (obtained through reading a piece of unparsed document) is numeric, without raising an error so that subsequent operations can be done.

Let's assume variable x contains the string to test.
We cannot do simply xs:integer($s), or any casting like that, because it would produce an fatal runtime error when not numeric.


So previously I was doing:
string(number($x)) = "NaN"

It is quite clear what the test wants to do, but it relies on "Not a Number" been represented by the "NaN" string. It is the default, but can be changed.
Then at maintenance stage, if someone changes number to format-number with another representation of NaN this could fail.


So now I'm using:
number($x) = number($x)

It's a bit unclear, but it is obvioulsy true when $x is an number, including + and - infinity (see spec.)
In fact, as the specification states, it is only false when $x is not a number, because NaN is never equal to NaN!


This test works fine (false is NaN, opposite to the first test), but it looks a little bit hard for maintenance.

Any better idea or advise ?

Best Regards.
Alain BENEDETTI


Current Thread