RE: [xsl] testing for string and number in XSLT 2.0 was Re: [xsl] Test For Numeric Values?

Subject: RE: [xsl] testing for string and number in XSLT 2.0 was Re: [xsl] Test For Numeric Values?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 9 Apr 2005 19:29:54 +0100
> EX. XML
> <?xml version="1.0" encoding="UTF-8"?>
> <example>
>     <test>132131</test>
> </example>

I'll assume there is no schema, that is, this is an untyped/unvalidated
document.

>     <xsl:variable name="x" select="example/test" as="xs:integer"/>
>     <xsl:variable name="y" select="example/test"/>
> 
>         y variable value(so we know we are selecting it): 132131
>         Test y as string:false
>         Test y as integer:false
>         x variable value:132131
>         test x as integer: true
> 
> not sure if this is what I would expect normally, the issue is related
> to an element if if has no explicitly declared data-type..

I don't know what your expectations are but these results are correct
according to the spec. If you don't validate the input document against a
schema, then the "typed value" of its nodes is untypedAtomic. If you test
($y instance of xdt:untypedAtomic) you will get the answer true.
untypedAtomic behaves essentially like XSLT 1.0 - if you use the value where
a string is expected, it's treated as a string, if you use it where an
integer is expected, it's converted to an integer.
> 
> it doesnt make much sense to me to *have* to declare something as an
> integer datatype to test if its a value is a number...whats the point?
> 
You need to distinguish "instance of" and "castable as". The "instance of"
operator is useful if you write a function that can accept arguments of
several different types and you want to test which type you have been given
(just like "instanceof" in Java). The "castable as" operator is useful when
you are given untyped data and you want to see whether its lexical form
makes it suitable for casting to a particular type such as xs:integer or
xs:date - which is where this thread started.

Michael Kay
http://www.saxonica.com/

Current Thread