RE: [xsl] Answers to review questions in "Beginning XSLT": Chapter 5

Subject: RE: [xsl] Answers to review questions in "Beginning XSLT": Chapter 5
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Tue, 11 Mar 2003 14:56:42 -0600
I had written:

>    Question: is there a way to represent the numeric constant NaN
>    in an expression?  Apparently the unquoted NaN is not recognized
>    as such; I guess it's interpreted as "the value of the 
> child element
>    named NaN."

but earlier:
> 10. To test whether the current node's value is a number,
>    use test="number() or number() = 0" which gives the desired result.
>    Is there a simpler way?
> 
*>    I tried test="number() != NaN" but this was always false (Why?  Is
*>      NaN not recognized as the numeric constant?  No, I just 
*> learned that
*>      it's because NaN is not equal to itself!  See the FAQ,

I was confused about whether you could use NaN in an expression
to mean the non-number.  After more experimentation, it seems that
the answer is no; much of the time it will produce what you want,
but it is not defined to do so and sometimes won't.

NaN is not "special".  It's just a name.  If the current node
has a child named NaN, that child's string value is returned as the value
of NaN.
Obviously then NaN will result in an empty node set most of the time,
which if used in a numeric context will be converted to NaN.
And in a boolean context the empty node set is equivalent to NaN.
But if used in a string context it will produce the empty string
instead of "NaN".

So, a correct way to get NaN in your expression would be to use
"number('')" (or "number('NaN')"; see Kay pp. 89-90.)

Lars


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


Current Thread