koral
koral

Reputation: 2945

XML Schema for collection of components of any of defined type

I have defined components of types inputTextType and inputEmailType (their definitions are not important here). I need to have components collection of any number inputText and/or inputEmail, in any order (example: text, email, text, text). I've tried:

<xs:complexType name="componentsType">
    <xs:choice>
        <xs:element name="inputText" type="inputTextType" maxOccurs="unbounded" minOccurs="0"/>
        <xs:element name="inputEmail" type="inputEmailType" maxOccurs="unbounded" minOccurs="0"/>
    </xs:choice>
</xs:complexType>

but then when validating XML

<components>
    <inputText>
        <label>label 1</label>
        <name>as</name>
    </inputText>
    <inputText>
        <label>label 2</label>
        <name>ghyyy6</name>
    </inputText>
    <inputEmail>
        <label>drg</label>
        <name>rgtght</name>
    </inputEmail>
</components>

I have an error cvc-complex-type.2.4.a: Invalid content was found starting with element 'inputEmail'. One of '{inputText}' is expected.. XML is correct (I need it). How should I fix the XSD?

For the XML above I changed XSD to

<xs:complexType name="componentsType">
    <xs:choice>
        <xs:element name="inputText" type="inputTextType" maxOccurs="unbounded" minOccurs="0"/>
        <xs:element name="inputEmail" type="inputEmailType" maxOccurs="unbounded" minOccurs="0"/>
    </xs:choice>
</xs:complexType>

and it worked but then I'm not able to define text, email, text, text but only first all texts and then all emails.

Upvotes: 1

Views: 160

Answers (1)

kjhughes
kjhughes

Reputation: 111491

Move the occurrence constraints up to xs:choice:

<xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="inputText" type="inputTextType"/>
    <xs:element name="inputEmail" type="inputEmailType"/>
</xs:choice>

Upvotes: 1

Related Questions