This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: NaN
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "Clark, Jason" <jason dot clark at tfn dot com>
- Cc: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 26 Feb 2002 18:06:02 +0000
- Subject: Re: [xsl] NaN
- Organization: Jeni Tennison Consulting Ltd
- References: <C11C167FF974D211A22800A0C9CFBA25048EC4B2@tfsmdmsg2.cda.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
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