RE: [xsl] Checking for instance of xs:integer

Subject: RE: [xsl] Checking for instance of xs:integer
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 12 Oct 2004 18:13:28 +0100
xsl:value-of constructs a text node, the value of the select expression is
atomized and converted to a string. By the time the xsl:element instruction
is evaluated, there is no remaining knowledge that the value of this text
node started life as an integer. The type annotation on the element will
therefore be "untyped". In fact, with a non-schema-aware XSLT processor, all
elements are annotated as "untyped".

With a schema-aware processor, you can annotate the element as an integer by
using the validation or type attributes on xsl:element, for example you can
say

<xsl:element name="a" type="xs:integer">

However, even then you need to be careful because your xsl:variable is
constructing an untyped document, and the type annotation on the element
will therefore be lost before you refer to it. What you want is:

<xsl:variable name="var" as="element(*, xs:integer)">
  <xsl:element name="a" type="xs:integer">12</xsl:element>
</xsl:variable>

<xsl:if test="data($a) instance of xs:integer">... true

<xsl:if test="$a instance of element(*, xs:integer)">... true

Michael Kay
http://www.saxonica.com

 

> -----Original Message-----
> From: Kenneth Stephen [mailto:marvin.the.cynical.robot@xxxxxxxxx] 
> Sent: 11 October 2004 19:01
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Checking for instance of xs:integer
> 
> Hi,
> 
>     I have the following code :
> 
> <?xml version="1.0"?>
> <xsl:stylesheet version="2.0"
>         xmlns:xs="http://www.w3.org/2001/XMLSchema";
>         xmlns:xdt="http://www.w3.org/2003/11/xpath-datatypes";
>         xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 
>         <xsl:output omit-xml-declaration="yes" />
> 
>         <xsl:template match="/">
>                 <xsl:variable name="var">
>                         <xsl:element name="a">
>                                 <xsl:value-of select="xs:integer(2)
> treat as xs:integer" />
>                         </xsl:element>
>                 </xsl:variable>
> 
>                 <xsl:choose>
>                         <xsl:when test="$var/a instance of 
> xs:integer">
>                                 yes
>                         </xsl:when>
>                         <xsl:otherwise>
>                                 no
>                         </xsl:otherwise>
>                 </xsl:choose>
>         </xsl:template>
> 
> </xsl:stylesheet>
> 
> ...where I'm trying to detect if a variable has an integer value. The
> output that I currently get is :
> 
> bash-2.05b$ java net.sf.saxon.Transform intck.xsl intck.xsl 
> 
>                                 no
> 
>     At first I thought that this is because the xsl:value-of that is
> used to assign the value of $var/a was producing either a string or an
> untypedAtomic value. However, changing the "xsl:when" to test for
> xs:string and xsd:untypedAtomic didnt work either. Is there any way to
> get this to work without using a schema aware processor?
> 
> Thanks,
> Kenneth

Current Thread