[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|