W3C home > Mailing lists > Public > xmlschema-dev@w3.org > December 2009

RE: Extending "choice" model group in a xsd schema

From: Michael Kay <mike@saxonica.com>
Date: Wed, 30 Dec 2009 16:26:20 -0000
To: "'Jaikiran Pai'" <jai_forums2005@yahoo.co.in>, <xmlschema-dev@w3.org>
Message-ID: <45FD70F4718A4F40B8CBAEC8DA9F5E4A@Sealion>

Derivation by extension allows you to create a type that allows the original
content, with new content added at the end. It doesn't allow any other
modification that produces content models which the original type won't
accept.

I've never been quite sure whether there is any theoretical justification
for allowing this kind of extension of a content model and no other kind,
but that's what the spec does. I assume it's by analogy with object-oriented
inheritance which allows a subtype to have additional properties that were
not present in the supertype.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay  

> -----Original Message-----
> From: xmlschema-dev-request@w3.org 
> [mailto:xmlschema-dev-request@w3.org] On Behalf Of Jaikiran Pai
> Sent: 28 December 2009 14:29
> To: xmlschema-dev@w3.org
> Subject: Extending "choice" model group in a xsd schema
> 
> Hello everyone,
> 
> I have been trying to "extend" a type which allows xsd:choice 
> model group to add a couple of more allowed choice elements. 
> Here's the original xsd:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
>             targetNamespace="http://www.myapp.org/xml/ns/myapp"
>             xmlns:myapp="http://www.myapp.org/xml/ns/myapp"
>             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>             elementFormDefault="qualified"
>             attributeFormDefault="unqualified"
>             version="1.0">
> 
>     <xsd:element name="graphics" type="graphicsType/>
> 
>     <xsd:complexType name="graphicsType">
>         <xsd:sequence>
>             <xsd:element name="colours" type="coloursType"/>
>         </xsd:sequence>
>     </xsd:complexType>
>     
> 
>     <xsd:complexType name="coloursType">
>         <xsd:choice maxOccurs="unbounded">
>               <xsd:element name="red">
>             <xsd:complexType />
>         </xsd:elemen>
>         <xsd:element name="blue">
>             <xsd:complexType />
>         </xsd:element>
>        </xsd:choice>
>     </xsd:complexType>
> </xsd:schema>
> 
> 
> The extended xsd is as follows: 
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
>             targetNamespace="http://www.myapp.org/xml/ns/myapp"
>             xmlns:myapp="http://www.myapp.org/xml/ns/myapp"
>             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>             elementFormDefault="qualified"
>             attributeFormDefault="unqualified"
>             version="1.0">
> 
>     <xsd:element name="extended-graphics" 
> type="extended-graphicsType/>
>     
>     <xsd:complexType name="extended-graphicsType">
>         <xsd:extension base="graphicsType">
>             <xsd:sequence>
>                 <xsd:element name="brush" type="xsd:string"/>
>             </xsd:sequence>
>         </xsd:extension>
>     </xsd:complexType>
> 
>     <xsd:complexType name="more-coloursType" >
>         <xsd:extension base="coloursType">
>                 <xsd:choice maxOccurs="unbounded">
>                       <xsd:element name="dark-red">
>                     <xsd:complexType />
>                 </xsd:elemen>
>                 <xsd:element name="dark-blue">
>                     <xsd:complexType />
>                 </xsd:element>
>                </xsd:choice>
>         </xsd:extension>
>     </xsd:complexType>
>         
> </xsd:schema>
> 
> So here i have a "coloursType" with a choice of "red" and 
> "blue" and the extended "more-coloursType" has the additional 
> "dark-red" and "dark-blue". Then in my xml i am trying to do this:
> 
> <extended-graphics xmlns="http://www.myapp.org/xml/ns/myapp"
>              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>              xmlns:myapp="http://www.myapp.org/xml/ns/myapp">
> 
>     <colours xsi:type="more-coloursType">
>         <dark-red/>
>     </colour>                 
>                  
> </extended-graphics>     
>  
> 
> So in the xml, i am trying to use the extended 
> "more-coloursType" and specifying a "dark-red" value. But 
> this fails validation, with a message saying that "dark-red" 
> is not allowed in this place. It starts working if i first 
> have a normal colour (either red or blue) and then have dark-red: 
> 
> <colours xsi:type="more-coloursType">
>         <blue/>
>         <dark-red/>
>     </colour>   
> 
> My understanding was with this kind of extension, with the 
> "choice" elements i would have something like: 
> 
> (red | blue)* (dark-red | dark-blue)*
> 
> but this doesn't seem to be the case. Am i missing something? 
> Are there any changes that i'll have to make to my xsd to allow: 
> 
>      <colours xsi:type="more-coloursType">
>         <dark-red/>
>     </colour>  
> 
> regards,
> -Jaikiran
> 
> 
> 
>       The INTERNET now has a personality. YOURS! See your 
> Yahoo! Homepage. http://in.yahoo.com/
> --0-1910538151-1262010512=0684
> Content-Type: text/html; charset=o-8859-1
> Content-Transfer-Encoding: quoted-printable
> 
> <table cellspacing="0" cellpadding="0" border="0" ><tr><td 
> valign="top" style="font: inherit;">Hello everyone,<br><br>I 
> have been trying to "extend" a type which allows xsd:choice 
> model group to add a couple of more allowed choice elements. 
> Here's the original xsd:<br><br>&lt;?xml version="1.0" 
> encoding="UTF-8"?&gt;<br>&lt;xsd:schema 
> xmlns="http://www.w3.org/2001/XMLSchema"<br>&nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> targetNamespace="http://www.myapp.org/xml/ns/myapp"<br>&nbsp;&
> nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> xmlns:myapp="http://www.myapp.org/xml/ns/myapp"<br>&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"<br>&nbsp;&nbsp;&n
> bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> elementFormDefault="qualified"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs
> p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>  
> attributeFormDefault="unqualified"<br>&nbsp;&nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> version="1.0"&gt;<br><br>&nbsp;&nbsp; &nbsp;&lt;xsd:element 
> name="graphics" type="graphicsType/&gt;<br><br>&nbsp;&nbsp; 
> &nbsp;&lt;xsd:complexType 
> name="graphicsType"&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:sequence&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:element name="colours" 
> type="coloursType"/&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:sequence&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:complexType&gt;<br>&nbsp;&nbsp; 
> &nbsp;<br><br>&nbsp;&nbsp; &nbsp;&lt;xsd:complexType 
> name="coloursType"&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:choice 
> maxOccurs="unbounded"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:element 
> name="red"&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:complexType 
> /&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  
> &nbsp;&lt;/xsd:elemen&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:element name="blue"&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:complexType /&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:element&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &lt;/xsd:choice&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:complexType&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;<br>&lt;/xsd:schema&gt;<br><br><br>The extended xsd is 
> as follows: <br><br>&lt;?xml version="1.0" 
> encoding="UTF-8"?&gt;<br>&lt;xsd:schema 
> xmlns="http://www.w3.org/2001/XMLSchema"<br>&nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> targetNamespace="http://www.myapp.org/xml/ns/myapp"<br>&nbsp;&
> nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> xmlns:myapp="http://www.myapp.org/xml/ns/myapp"<br>&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>  
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"<br>&nbsp;&nbsp;&n
> bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> elementFormDefault="qualified"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs
> p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> attributeFormDefault="unqualified"<br>&nbsp;&nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> version="1.0"&gt;<br><br>&nbsp;&nbsp; &nbsp;&lt;xsd:element 
> name="extended-graphics" 
> type="extended-graphicsType/&gt;<br>&nbsp;&nbsp; 
> &nbsp;<br>&nbsp;&nbsp; &nbsp;&lt;xsd:complexType 
> name="extended-graphicsType"&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:extension 
> base="graphicsType"&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:sequence&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:element name="brush" 
> type="xsd:string"/&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:sequence&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&lt;/xsd:extension&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:complexType&gt;<br><br>&nbsp;&nbsp; 
> &nbsp;&lt;xsd:complexType name="more-coloursType" 
> &gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:extension 
> base="coloursType"&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:choice 
> maxOccurs="unbounded"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:element 
> name="dark-red"&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:complexType /&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:elemen&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;xsd:element 
> name="dark-blue"&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;xsd:complexType  /&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:element&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>  &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;/xsd:choice&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:extension&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/xsd:complexType&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;<br>&lt;/xsd:schema&gt;<br><br>So 
> here i have a "coloursType" with a choice of "red" and "blue" 
> and the extended "more-coloursType" has the additional 
> "dark-red" and "dark-blue". Then in my xml i am trying to do 
> this:<br><br>&lt;extended-graphics 
> xmlns="http://www.myapp.org/xml/ns/myapp"<br>&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
> nbsp; 
> xmlns:myapp="http://www.myapp.org/xml/ns/myapp"&gt;<br><br>&nb
> sp;&nbsp;
>  &nbsp;&lt;colours 
> xsi:type="more-coloursType"&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;dark-red/&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/colour&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
> p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> &nbsp;<br>&lt;/extended-graphics&gt;&nbsp;&nbsp;&nbsp; 
> &nbsp;<br>&nbsp;<br><br>So in the xml, i am trying to use the 
> extended "more-coloursType" and specifying a "dark-red" 
> value. But this fails validation, with a message saying that 
> "dark-red" is not allowed in this place. It starts working if 
> i first have a normal colour (either red or blue) and then 
> have dark-red: <br><br>&lt;colours 
> xsi:type="more-coloursType"&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;blue/&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;dark-red/&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/colour&gt;&nbsp; &nbsp;<br><br>My understanding 
> was with this kind  of extension, with the "choice" elements 
> i would have something like: <br><br>(red | blue)* (dark-red 
> | dark-blue)*<br><br>but this doesn't seem to be the case. Am 
> i missing something? Are there any changes that i'll have to 
> make to my xsd to allow: <br><br>&nbsp;&nbsp;&nbsp;&nbsp; 
> &lt;colours xsi:type="more-coloursType"&gt;<br>&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&lt;dark-red/&gt;<br>&nbsp;&nbsp; 
> &nbsp;&lt;/colour&gt;&nbsp; 
> <br><br>regards,<br>-Jaikiran<br></td></tr></table><br>
> 
> 
> 
>       <!--1--><hr size=1></hr>
> The INTERNET now has a personality. YOURS! <a 
> href="http://in.rd.yahoo.com/tagline_yyi_1/*http://in.yahoo.co
> m/" target="_blank">See your Yahoo! Homepage</a>.
> --0-1910538151-1262010512=0684--
> 
> 
Received on Wednesday, 30 December 2009 16:26:49 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:17 UTC