W3C home > Mailing lists > Public > xmlschema-dev@w3.org > June 2004

RE: Restricted type "derivation" help - concrete example within

From: Michael Kay <mhk@mhk.me.uk>
Date: Wed, 23 Jun 2004 18:57:24 +0100
To: "'David Aldridge'" <daldridge@ncaustin.com>, <xmlschema-dev@w3.org>
Message-Id: <20040623175803.F1A28A494B@frink.w3.org>
You can't have two elements in the same sequence with the same name but
different types. You're going to have to give the elements different names.
 
Michael Kay


  _____  

From: xmlschema-dev-request@w3.org [mailto:xmlschema-dev-request@w3.org] On
Behalf Of David Aldridge
Sent: 23 June 2004 17:48
To: xmlschema-dev@w3.org
Subject: Restricted type "derivation" help - concrete example within


I have an old C++ class heirarchy that I'm trying to migrate to being mostly
defined via XML rather than C++ code, and have run into a snag with
specifying the appropriate type in my schema.  Rather than explaining it in
prose, below are example .XSD and .XML implementations that illustrate the
issue.  My comments and question follow the source.
 
test.xsd
======================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
 
<xs:complexType name="ColorType">
    <xs:sequence>
        <xs:element name="red" type="xs:unsignedByte"/>
        <xs:element name="green" type="xs:unsignedByte"/>
        <xs:element name="blue" type="xs:unsignedByte"/>
        <xs:element name="alpha" type="xs:unsignedByte"/>
    </xs:sequence>
</xs:complexType>
 
<xs:complexType name="ShapeType" abstract="true">
    <xs:sequence>
        <xs:element name="color" type="ColorType"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:element name="Shape" type="ShapeType" abstract="true"/>
 
<xs:complexType name="CircleType">
    <xs:complexContent>
        <xs:extension base="ShapeType">
            <xs:sequence>
                <xs:element name="radius" type="xs:unsignedByte"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:element name="Circle" type="CircleType" substitutionGroup="Shape"/>
 
<xs:complexType name="SquareType">
    <xs:complexContent>
        <xs:extension base="ShapeType">
            <xs:sequence>
                <xs:element name="length" type="xs:unsignedByte"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:element name="Square" type="SquareType" substitutionGroup="Shape"/>
 
<xs:complexType name="MickeyMouseHeadType1">
    <xs:complexContent>
        <xs:extension base="ShapeType">
            <xs:sequence>
                <xs:element name="LeftEar" type="CircleType"/>
                <xs:element name="RightEar" type="CircleType"/>
                <xs:element name="Head" type="CircleType"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:element name="MickeyMouseHead1" type="MickeyMouseHeadType1"
substitutionGroup="Shape"/>
 
<!--xs:complexType name="MickeyMouseHeadType2">
    <xs:complexContent>
        <xs:extension base="ShapeType">
            <xs:sequence>
                <xs:element ref="Circle">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:restriction base="CircleType">
                                <xs:attribute name="name" type="xs:string"
use="required" fixed="LeftEar"/>
                            </xs:restriction>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
                <xs:element ref="Circle">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:restriction base="CircleType">
                                <xs:attribute name="name" type="xs:string"
use="required" fixed="RightEar"/>
                            </xs:restriction>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
                <xs:element ref="Circle">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:restriction base="CircleType">
                                <xs:attribute name="name" type="xs:string"
use="required" fixed="Head"/>
                            </xs:restriction>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:element name="MickeyMouseHead2" type="MickeyMouseHeadType2"
substitutionGroup="Shape"/-->
 
<xs:element name="Canvas">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Shape" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
 
</xs:schema>
=================
 
test.xml
=================
<?xml version="1.0" encoding="UTF-8"?>
<Canvas xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
    <Circle name="Circle1">
        <color>
            <red>1</red>
            <green>2</green>
            <blue>3</blue>
            <alpha>4</alpha>
        </color>
        <radius>5</radius>
    </Circle>
     <MickeyMouseHead1 name="MMHead1">
         <color>
             <red>11</red>
             <green>22</green>
             <blue>33</blue>
             <alpha>44</alpha>
         </color>
         <LeftEar name="LeftEar">
             <color>
                 <red>1</red>
                 <green>2</green>
                 <blue>3</blue>
                 <alpha>4</alpha>
             </color>
             <radius>5</radius>
         </LeftEar>
         <RightEar name="RightEar">
             <color>
                 <red>1</red>
                 <green>2</green>
                 <blue>3</blue>
                 <alpha>4</alpha>
             </color>
             <radius>5</radius>
         </RightEar>
         <Head name="Head">
             <color>
                 <red>1</red>
                 <green>2</green>
                 <blue>3</blue>
                 <alpha>4</alpha>
             </color>
             <radius>5</radius>
         </Head>
     </MickeyMouseHead1>
</Canvas>
=================
 
 
COMMENTS / QUESTIONS:
 
The schema and xml content are valid (note that 'MickeyMouseHeadType2' is
commented out, as it in invalid content), but not sufficient for my C++
loading code, as it knows how to create/instantiate <Circle> and <Square>s,
not <LeftEar>, <RightEar>, and <Head>s.  I would *prefer* to come up with a
workable solution for 'MickeyMouseHeadType2', as that would allow .XML
thusly:
 
...
     <MickeyMouseHead2 name="MMHead2">
         <color ... />
         <Circle name="LeftEar"> ...(contents)... </Circle>
         <Circle name="RightEar"> ...(contents)... </Circle>
         <Circle name="Head"> ...(contents)... </Circle>
     </MickeyMouseHead2>
...
 
Any ideas?
 
Thanks!
David
Received on Wednesday, 23 June 2004 13:58:05 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:44 GMT