xs:alternative & reuse?

This should cover W3C XML Schema, Relax NG and DTD related problems.
voxtech
Posts: 1
Joined: Wed Apr 15, 2020 5:14 am

xs:alternative & reuse?

Post by voxtech » Wed Apr 15, 2020 5:17 am

Hello,

I have a complex bit of polymorphism I've achieved via xs:alternative in XSD 1.1. However, I'd like this weird amalgamated "type" I've created to be used in multiple elements. This is a problem, since as far as I can tell, the xs:alternative tag can only be a child of xs:element. Is there any way to do code re-use here, where I can have a the set of alternatives defined in a single place and multiple elements use it?

tavy
Posts: 234
Joined: Thu Jul 01, 2004 12:29 pm

Re: xs:alternative & reuse?

Post by tavy » Wed Apr 15, 2020 3:07 pm

Hello,

You cannot reuse xs:alternative, you can reuse the types referred in the alternative. Here is an example:

Code: Select all

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

    <xs:element name="Example">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Beverage" type="BeverageType" maxOccurs="unbounded">
                    <xs:alternative test="@current-time le '12:00:00'" type="MorningBeverage" />
                    <xs:alternative test="@current-time gt '12:00:00'" type="AfternoonBeverage" />
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="BeverageType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="current-time" type="xs:time" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="MorningBeverage">
        <xs:simpleContent>
            <xs:extension base="BeverageType">
                <xs:attribute name="tea" type="xs:string" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="AfternoonBeverage">
        <xs:simpleContent>
            <xs:extension base="BeverageType">
                <xs:attribute name="juice" type="xs:string" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply