alvilla
alvilla

Reputation: 23

XSD choice of sequences

I want an element <aaa> to contain either a sequence of element <bbb> or a sequence of two elements <bbb>,<ccc>. For example, the following is valid:

<aaa>
  <bbb>
  <bbb>
</aaa>

and

<aaa>
  <bbb>
  <ccc>
  <bbb>
  <ccc>
</aaa>

but the following is invalid:

<aaa>
  <bbb>
  <ccc>
  <bbb>
</aaa>

I have tried the following scheme

<xs:complexType name="aaa">
  <xs:choice minOccurs="0">
        <xs:sequence  minOccurs="0" maxOccurs="15">             
            <xs:element name="bbb" type="xxx" /> 
            <xs:element name="ccc" type="xxx"/>
        </xs:sequence> 
        <xs:sequence  minOccurs="0" maxOccurs="15">
            <xs:element name="bbb" type="xxx" />
        </xs:sequence>
  </xs:choice>
</xs:complexType>

but I have this error message when validating with JAXP: cos-nonambig: bbb and bbb (or elements from their substitution group) violate "Unique Particle Attribution" Is there a way that I can obtain my goal? Thanks in advance

Upvotes: 2

Views: 6394

Answers (1)

sergioFC
sergioFC

Reputation: 6016

I think that error wouldn't appear if you used XSD 1.1 (can't test it right now). Edit: thanks user Michael Kay for commenting that in XSD 1.1 the schema still have to be unambiguous, so (unlike I thought) Unique Particle Attribution error would still occur in XSD 1.1.

Anyway you can do it like the following example. Using this you're saying that the content of an aaa element always start with a bbb element, that is followed by either

a) from 0 to infinity bbb elements

or

b) a ccc element and optionaly n times the sequence bbb, ccc

<xs:element name="aaa">
    <xs:complexType>
        <xs:sequence minOccurs="0">
            <xs:element name="bbb" type="xxx" />
            <xs:choice>
                <xs:element name="bbb" type="xxx" minOccurs="0" maxOccurs="unbounded" />
                <xs:sequence>
                    <xs:element name="ccc" type="xxx" />
                    <xs:sequence minOccurs="0" maxOccurs="unbounded">
                        <xs:element name="bbb" type="xxx" />
                        <xs:element name="ccc" type="xxx" />
                    </xs:sequence>
                </xs:sequence>
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Upvotes: 3

Related Questions