ulab
ulab

Reputation: 1089

Does xsd:choice require one or the other, or are both allowed too?

I have two complex types and an element in an XML can take any of these two types. And sometimes, I know which one of these type the element can take. So accordingly I define in XSD.

<xs:complexType name="A">
    <xs:sequence>
        <xs:element name="one" type="onetype" minOccurs="0"
            maxOccurs="1" />
        <xs:element name="two" type="twotype" minOccurs="0"
            maxOccurs="unbounded" />
        <xs:element name="three" type="country" minOccurs="0"
            maxOccurs="1" />
    </xs:sequence>
    <xs:attribute name="oneattr" type="xs:string" />
    <xs:attribute name="twoattr" type="xs:string" />
</xs:complexType>

<xs:complexType name="B">
    <xs:sequence>
        <xs:element name="four" type="fourtype" minOccurs="0"
            maxOccurs="1" />
    </xs:sequence>
    <xs:attribute name="fourattr" type="xs:string" />
</xs:complexType>  

But sometimes i don't, so i have created another type like AorB.

<xs:complexType name="AorB">
        <xs:choice>
            <xs:sequence>
                <xs:element name="one" type="onetype" minOccurs="0"
                    maxOccurs="1" />
                <xs:element name="two" type="twotype" minOccurs="0"
                    maxOccurs="unbounded" />
                <xs:element name="three" type="country" minOccurs="0"
                    maxOccurs="1" />
            </xs:sequence>
            <xs:sequence>
                <xs:element name="four" type="fourtype" minOccurs="0"
                    maxOccurs="1" />
            </xs:sequence>
        </xs:choice>
        <xs:attribute name="oneattr" type="xs:string" />
        <xs:attribute name="twoattr" type="xs:string" />
        <xs:attribute name="fourattr" type="xs:string" />
    </xs:complexType>

So my question is this: Of the two sequences inside xsd:choice, when the XML is validated, does it validate true if XML contains elements from both sequence or only one ?

Upvotes: 1

Views: 2562

Answers (1)

kjhughes
kjhughes

Reputation: 111541

xsd:choice requires one or the other

when the XML is validated, does it validate true if XML contains elements from both sequence or only one ?

The default of @minOccurs on xsd:choice is 1, which means one or the other, not both, of the children content models apply. If you add minOccurs="value" for value greater than 1 or for value equal to unbounded, then multiple occurrences of its children are allowed. Note, however, that the second occurrence might be a repeat of the child chosen from the first occurrence.

Upvotes: 1

Related Questions