W3C home > Mailing lists > Public > xmlschema-dev@w3.org > September 2010

Re: overriding sub-elements with XSD

From: Kevin Braun <kbraun@obj-sys.com>
Date: Wed, 29 Sep 2010 13:45:59 -0400
Message-ID: <4CA37B57.5040805@obj-sys.com>
To: trubliphone@googlemail.com
CC: xmlschema-dev@w3.org

You probably don't want to think in terms of OO here (or perhaps you 
do...you don't override fields...).  Think of extension as adding 
attributes or appending content.  Think of restriction as restricting 
content or attributes.

In your case, which is probably simplified from your real scenario, you 
could just have subElement in the child with minOccurs="1", 
maxOccurs="1".  Then, a ChildClass must have two subElements: the one 
from the Parent, and then the one from the Child.  However, note that 
the contribution from the ChildClass must come at the end, so if your 
real scenario has other elements involved, this may not give you the 
ordering you want.

You might be able to make use of restriction.  subElement{1,1} and 
subElement{2,2} are both restrictions of subElement{1, 2} (using 
{min,max} notation).  In the first case, you are allowing fewer 
subElements (max becomes only 1), in the second case you are requiring 
more (min becomes 2).  So, you might introduce a new Parent that allows 
1 to 2 subElements, and then have two restrictions of that type, one 
that forces 1 subElement and the other that forces 2.

I hope that helps some,
Objective Systems, Inc.

On 9/29/2010 12:58 PM, trubliphone wrote:
> Hello.  I have two complexTypes, one of which extends the other, and 
> both which have a sub-element with the same name.  However, the 
> details of that element (type, min/maxOccurs,, etc.) can vary.  In 
> this example below, "parent" must have one "subElement" and "child" 
> must have two:
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> <xs:complexType name="ParentClass">
> <xs:sequence>
> <xs:element name="subElement" minOccurs="1" maxOccurs="1"/>
> </xs:sequence>
> </xs:complexType>
> <xs:complexType name="ChildClass">
> <xs:complexContent>
> <xs:extension base="ParentClass">
> <xs:sequence>
> <xs:element name="subElement" minOccurs="2" maxOccurs="2"/>
> </xs:sequence>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:element name="root">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="parent" type="ParentClass"/>
> <xs:element name="child" type="ChildClass"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:schema>
> However, when I create an XML file, "child" actually has three 
> "subElements:" one from the "ParentClass" it extends and the two it 
> defines itself.  Does anybody know a way around this?  I am basically 
> hoping for an "oo-style" solution, where the child's sub-element 
> _overrides_ the parent's sub-element of the same name.  Is that 
> possible in XSD?
> Thanks.
Received on Wednesday, 29 September 2010 17:53:09 UTC

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