xsl-list
[Top] [All Lists]

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

2005-04-09 11:43:06
Michael Kay wrote:

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.
 

yes, like much of the xml in existence.

   <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"
 

I think I have enough understanding of the two.

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.
 


doing

        y variable value(so we know we are selecting it): <xsl:value-of
select="$y"/>
        Test y as string:<xsl:value-of select="$y castable as xs:string"/>
        Test y as integer:<xsl:value-of select="$y castable as xs:integer"/>

comes up with

        y variable value(so we know we are selecting it): 3123123
        Test y as string:true
        Test y as integer:true

back to my original question, how can I test if a value is a string or
integer (with the very real constraint of data not being explicitly typed).

not having a go, just think that this is a pretty valid use case.

--Jim Fuller

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--