Subject: Re: [xsl] [XSL 3.0] Function that check if a xs:string can be converted as a xs:double From: "Piez, Wendell A. (Fed) wendell.piez@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Wed, 31 May 2023 15:08:06 -0000 |
Hello, As an academic exercise it is still interesting to ponder what might be done without 'castable as'. I think it was Ken H who once suggested (context of XSLT 1.0) select="number($str) = number($str)" as NaN is not equal to itself. I can't say where he got it. (In XPath 1.0 one was forced to be creative.) Or whether it would work for you in the given context or indeed corresponds to 'castable as'. Regards, Wendell -----Original Message----- From: John Lumley john@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Sent: Wednesday, May 31, 2023 10:09 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] [XSL 3.0] Function that check if a xs:string can be converted as a xs:double On 31/05/2023 13:13, Christophe Marchand cmarchand@xxxxxxxxxxxxxx wrote: > I've also found xs:boolean(number($s)) is working correctly. I would strongly suggest that you use the $s castable as xs:double construct rather than this one. As has been pointed out it fails for s="0", as the boolean value of a zero is false. But you've also been somewhat misled by getting correct answers with e.g.s="A string". The definition of number() (https://www.w3.org/TR/xpath-functions-31/#func-number) shows that when supplied with a string which cannot be parsed as a number, in most circumstances rather than failing, it returns NaN (not a number), whose boolean value is then taken as false. When you wish to check whether something can be cast to a given type using the castable as operator (https://www.w3.org/TR/xpath-31/#id-castable) is much preferable, and much clearer. John Lumley
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] [XSL 3.0] Function that c, John Lumley john@xxx | Thread | Re: [xsl] [XSL 3.0] Function that c, David Carlisle d.p.c |
Re: [xsl] [XSL 3.0] Function that c, John Lumley john@xxx | Date | Re: [xsl] [XSL 3.0] Function that c, Michael Kay mike@xxx |
Month |