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

Re: [xsl] NaN


Subject: Re: [xsl] NaN
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 26 Feb 2002 18:06:02 +0000

Hi Jason,

> I want to see if the value of an attribute or element is a number or
> string. I tried using number(@value) != 'NaN', but it always returns
> false. How can I find if a value is a string or number.

I think that you mean it always returns true? When you compare a
number (e.g. number(@value)) with a string (e.g. 'NaN'), then the
string gets converted to a number (so 'NaN' becomes NaN), and the
comparison is made. So if @value has the value 25 then the comparison
is between 25 and NaN, and 25 is not equal to NaN, so it returns true.
However, NaN has a weird quality - NaN is not equal to *any* number,
including NaN. So NaN != NaN also returns true.

There are two ways, therefore, that you can test whether the value
attribute holds a number. First, you can compare the result of
converting it to a number and then to a string with the string 'NaN':

  string(number(@value)) != 'NaN'

This will return true if @value is a number (because '25' is not equal
to 'NaN') and false if @value is not a number (because the string
'NaN' is equal to the string 'NaN').

Alternatively, you can use:

  number(@value) = number(@value)

This will return true if @value is a number (because 25 is equal to
25) and false if @value is not a number (because the number NaN is not
equal to the number NaN).

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
  • [xsl] NaN
    • Clark, Jason - Tue, 26 Feb 2002 12:18:52 -0500
      • Jeni Tennison - Tue, 26 Feb 2002 18:06:02 +0000 <=
      • <Possible follow-ups>
      • G. Ken Holman - Tue, 26 Feb 2002 12:50:50 -0500