xsl-list
[Top] [All Lists]

RE: [xsl] [XSLT 2.0] Checking that an element's value has the desired datatype?

2006-10-16 08:51:29
Excellent!  With this: 

    matches(flt:Aircraft/flt:Altitude, '[0-9]+') 

I can do datatype checking, without using XML Schemas.

This will be very handy for Schematron ...

/Roger

-----Original Message-----
From: Michael Kay [mailto:mike(_at_)saxonica(_dot_)com] 
Sent: Monday, October 16, 2006 11:47 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] [XSLT 2.0] Checking that an element's value has the
desired datatype?

If there's no schema, then the Altitude element is untyped, so applying
data() to it gives an instance of xs:untypedAtomic, not an integer. 

The expression you want is

 flt:Aircraft/flt:Altitude castable as xsd:integer

which tests not whether the value is an integer, but whether conversion
to
an integer would succeed.

You could also test this with a regular expression

matches(flt:Aircraft/flt:Altitude, '[0-9]+')

(which in Saxon is probably faster, as "castable as" will tend to throw
and
then catch a Java exception in the "false" path, and throwing
exceptions is
very inefficient in Java).
 
Michael Kay
http://www.saxonica.com/


-----Original Message-----
From: Costello, Roger L. [mailto:costello(_at_)mitre(_dot_)org] 
Sent: 16 October 2006 16:34
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] [XSLT 2.0] Checking that an element's value 
has the desired datatype?

Hi Folks,

Below is an XML document containing information about the 
Altitude of an Aircraft.  

I have written a stylesheet to check the Altitude's value, to 
see if it is an integer.  Below is my stylesheet.

My stylesheet uses this statement:

   <xsl:value-of select="data(flt:Aircraft/flt:Altitude) 
instance of xsd:integer"/>

The output I get is: "false"

(The output I seek is "true", as the Altitude element does 
have an integer value.)  

Can someone tell me the correct way to do this?  

Thanks!  /Roger 

--------------------------------------------------------------
---------
----------
<?xml version="1.0"?>
<Flight xmlns="http://www.aviation.org";>
    <Aircraft>
        <Altitude>3300</Altitude>
    </Aircraft>
</Flight>
--------------------------------------------------------------
---------
----------
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       xmlns:flt="http://www.aviation.org";
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema";
                       version="2.0">

    <xsl:output method="html"/>

    <xsl:template match="flt:Flight">
      <html>
        <body>
            Check that the aircraft's altitude is an integer:
            <xsl:value-of select="data(flt:Aircraft/flt:Altitude)
instance of xsd:integer"/>
        </body>
      </html>
    </xsl:template>

</xsl:stylesheet>

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



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


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

<Prev in Thread] Current Thread [Next in Thread>