Re: [xsl] Pure Union Type

Subject: Re: [xsl] Pure Union Type
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 4 May 2014 13:32:24 -0000
The cause of the problem here is that your numeric type does not include
xs:decimal as one of its member types; it only includes an anonymous type
derived from decimal by restriction. The number 3.3 is not an instance of this
anonymous type, and therefore it is not an instance of the union.

My first thought was to define the union as

            <xs:union>
                <xs:simpleType ref="xs:decimal"/>
                <xs:simpleType ref="xs:integer"/>
                <xs:simpleType ref="xs:float"/>
                <xs:simpleType ref="xs:double"/>
            </xs:union>

But for some reason XSD doesn't allow this; it has to be

            <xs:union memberTypes="xs:decimal xs:integer xs:float
xs:double"/>

This should now work, but it hits a Saxon bug:

java.lang.ClassCastException: com.saxonica.ee.schema.UserUnionType cannot be
cast to net.sf.saxon.type.AtomicType

The type checking code for arithmetic expressions isn't allowing for the
statically-inferred type of the argument to be a union type. I will fix this.

Unless you have any objections I will add your test case as import-schema-193
to the W3C XSLT 3.0 test suite.

Michael Kay
Saxonica

On 4 May 2014, at 05:31, Dimitre Novatchev dnovatchev@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> <xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:my="my:my"
> exclude-result-prefixes="xs my">
> <xsl:output method="text"/>
> <xsl:import-schema>
>     <xs:schema xsl:validation="strict">
>         <xs:simpleType name="numeric">
>             <xs:union>
>                 <xs:simpleType>
>                     <xs:restriction base="xs:decimal"/>
>                 </xs:simpleType>
>                 <xs:simpleType>
>                     <xs:restriction base="xs:integer"/>
>                 </xs:simpleType>
>                 <xs:simpleType>
>                     <xs:restriction base="xs:float"/>
>                 </xs:simpleType>
>                 <xs:simpleType>
>                     <xs:restriction base="xs:double"/>
>                 </xs:simpleType>
>             </xs:union>
>         </xs:simpleType>
>     </xs:schema>
> </xsl:import-schema>
> <xsl:template match="/">
>     <xsl:sequence select="3 castable as numeric"/>
>     =============================================
>     <xsl:sequence select="my:add(3.3 cast as numeric, 5.787 cast as
numeric)"/>
> </xsl:template>
>
> <xsl:function name="my:add" as="numeric">
>     <xsl:param name="pLeft" as="numeric"/>
>     <xsl:param name="pRight" as="numeric"/>
>
>     <xsl:sequence select="($pLeft + $pRight) cast as numeric"/>
> </xsl:function>
> </xsl:stylesheet>

Current Thread