[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: testing for string and number in XSLT 2.0 was Re:

Subject: Re: testing for string and number in XSLT 2.0 was Re: Test For Numeric Values?
From: James Fuller <jim.fuller@xxxxxxxxxxxxxx>
Date: Sat, 09 Apr 2005 20:43:06 +0200
xslt if string
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.