Re: overriding sub-elements with XSD

Hi,

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,
Kevin
-- 
Objective Systems, Inc.
REAL WORLD ASN.1 AND XML SOLUTIONS
http://www.obj-sys.com


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