Ankush
Ankush

Reputation: 827

XSD: ComplexType

I have a complexType like:

<xsd:complexType name="NightlyRate">
    <xsd:complexContent>
        <xsd:extension base="com:Money">
            <xsd:attribute name="night" type="com:Number" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Now, I want to add a child element to "NightlyRate", which itself is of complexType. I tried adding:

<xsd:element name="xxx" type"com:Money"/>

after the complexContent element, but it throws error that element is not expected, also part of the problem is type of this complex-element is the same as type of extension base. I am using JAXB. Is there any other way to achieve this?

Thanks!

Upvotes: 0

Views: 3345

Answers (2)

womp
womp

Reputation: 116977

<xsd:complexType name="NightlyRate">
    <xsd:complexContent>
        <xsd:extension base="com:Money">
            <xsd:sequence>
                <xsd:element name="xxx" type"com:Money">
                   <xsd:complexType>
                      <xsd:complexContent>
                             /// Attributes here
                      </xsd:complexContent>
                   </xsd:complexType>
                </xsd:element>
            </xsd:sequence>

            <xsd:attribute name="night" type="com:Number" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Upvotes: 1

instanceof me
instanceof me

Reputation: 39138

To get sub-elements in a xsd:complexType, you have to put a xsd:sequence in it (first level, along with annotations and attributes), wich states that you want well... sub-elements. In this sequence, you can include elements of other defined types or define new types.

See XSD complex elements at W3Schools, and womp's example.

In some cases, you may want to use the xsd:any element to allow yet-undefined extensions (loose).

Upvotes: 2

Related Questions