This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: NaN


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]