Brian Cribbs
Brian Cribbs

Reputation: 23

XSD none, one, or both

I searched a whole bunch and couldn't find any questions similar to this before posting. I'm working on creating an XSD and one complexType has these requirements:

It essentially works out to "none, one(but a specific one), or both" but I can't figure out what I'm doing incorrectly and any help would be appreciated

<xs:complexType name="Foo">
    <xs:choice minOccurs="0">
        <xs:sequence>
            <xs:element name="fieldOne" maxOccurs="1"/>
        </xs:sequence>
        <xs:sequence>
            <xs:element name="fieldOne" maxOccurs="1"/>
            <xs:element name="fieldTwo" maxOccurs="1"/>             
        </xs:sequence>
    </xs:choice>
</xs:complexType>

Upvotes: 2

Views: 234

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122394

You could do this with an optional sequence:

<xs:complexType name="Foo">
    <xs:sequence minOccurs="0">
        <xs:element name="fieldOne"/>
        <xs:element name="fieldTwo" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

The only way for a fieldTwo to be present is for the sequence to occur once, in which case fieldOne is required as well.

Upvotes: 2

Related Questions