Xsd and inheritance

Simply add an <xsd:sequence> with the required elements:

<xsd:complexType name="B">  
    <xsd:complexContent>
        <xsd:extension base="A">
           <xsd:sequence>
              <xsd:element name="Hours">
              ...
              </xsd:element>
           </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

You will need to define options as a complex type of it's own, then use extension on that to create a new options complex type and use substitution instead of extension.


Here's the schema I came up with:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inheritance"
    targetNamespace="http://test.com"
    elementFormDefault="qualified"
    xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:test="http://test.com"
>
    <xs:element name="Time">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="First" type="test:A" />
                <xs:element name="Second" type="test:B" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="shortOptions">
        <xs:sequence>
            <xs:element name="Day" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="longOptions">
        <xs:complexContent>
            <xs:extension base="test:shortOptions">
                <xs:sequence>
                    <xs:element name="Week" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="A">
        <xs:sequence>
            <xs:element name="options" type="test:shortOptions" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="B">
        <xs:sequence>
            <xs:element name="options" type="test:longOptions" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Which seems to fit this xml:

<?xml version="1.0" encoding="utf-8" ?>
<Time xmlns="http://test.com">
    <First>
        <options>
            <Day>Today</Day>
        </options>
    </First>
    <Second>
        <options>
            <Day>Tomorrow</Day>
            <Week>This Week</Week>
        </options>
    </Second>
</Time>

Tags:

Xml

Xsd