- From: Mukul Gandhi <gandhi.mukul@gmail.com>
- Date: Thu, 30 Sep 2010 16:51:16 +0530
- 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
that.
> 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:complexType>
<xs:sequence>
<xs:element name="parent">
<xs:complexType>
<xs:group ref="TEST_GROUP" />
<xs:assert test="count(subElement) = 1" />
</xs:complexType>
</xs:element>
<xs:element name="child">
<xs:complexType>
<xs:group ref="TEST_GROUP" />
<xs:assert test="count(subElement) = 2" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:group name="TEST_GROUP">
<xs:sequence>
<xs:element name="subElement" minOccurs="0" maxOccurs="2" />
</xs:sequence>
</xs:group>
</xs:schema>
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
assertions.
The above schema would say successfully validate following XML
instance document:
<root>
<parent>
<subElement/>
</parent>
<child>
<subElement/>
<subElement/>
</child>
</root>
--
Regards,
Mukul Gandhi
Received on Thursday, 30 September 2010 11:22:09 UTC