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

Re: overriding sub-elements with XSD

From: Mukul Gandhi <gandhi.mukul@gmail.com>
Date: Thu, 30 Sep 2010 16:51:16 +0530
Message-ID: <AANLkTintixwZ8fHcDc64n_HfhirQoYbE7eBcxykMsd_m@mail.gmail.com>
To: xmlschema-dev@w3.org
Cc: trubliphone@googlemail.com
On Wed, Sep 29, 2010 at 10:28 PM, trubliphone
<trubliphone@googlemail.com> wrote:
> 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:

you don't have to necessarily model the specified XML document
structure as schema type-subtype relationship. Since derivation both
by schema extension and restriction makes achieving the objectives
specified in your use-case (the XML element hierarchy you want)
difficult (with at-least XSD 1.0). Kevin has explained quite a bit of

> 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?

I would rather like using following approach with XSD 1.1 assertions:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:element name="root">
                   <xs:element name="parent">
                           <xs:group ref="TEST_GROUP" />
                           <xs:assert test="count(subElement) = 1" />
                   <xs:element name="child">
                            <xs:group ref="TEST_GROUP" />
                            <xs:assert test="count(subElement) = 2" />

      <xs:group name="TEST_GROUP">
               <xs:element name="subElement" minOccurs="0" maxOccurs="2" />


Here we create a XSD group component (with the objective of reusing
the element particle definitions from schema group in more than one
schema types -- which I think is a useful design substitute of schema
type-subtype relation you're looking after). In the above schema
example, please note the cardinality definition here of "subElement"
in xs:group which is 0-2 (both inclusive) -- this allows us to control
cardinality of element particles at reuse points (here complex type
definitions of "child" & "parent" elements), via Schema 1.1

The above schema would say successfully validate following XML
instance document:


Mukul Gandhi
Received on Thursday, 30 September 2010 11:22:09 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:57 UTC