18

I am trying to make a simple XSD choice construct allowing either one or both of two referenced elements, but not none. The construct is similar to below but I keep getting an ambiguity error. What am I missing?

<xs:schema xmlns:xs="...">
  <xs:element name="Number" type="xs:integer"/>
  <xs:element name="Text" type="xs:string"/>
  <xs:element name="RootStructure">
    <xs:complexType>
      <xs:sequence>
        <xs:choice>
          <xs:sequence>
            <xs:element ref="Number"/>
            <xs:element ref="Text"/>
          </xs:sequence>
          <xs:element ref="Number"/>
          <xs:element ref="Text"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
lox
  • 1,602
  • 4
  • 27
  • 41

2 Answers2

32

The usual way to do it is this:

<xs:schema xmlns:xs="...">
  <xs:element name="Number" type="xs:integer"/>
  <xs:element name="Text" type="xs:string"/>
  <xs:element name="RootStructure">
    <xs:complexType>
      <xs:sequence>
        <xs:choice>
          <xs:sequence>
            <xs:element ref="Number"/>
            <xs:element ref="Text" minOccurs="0"/>
          </xs:sequence>
          <xs:element ref="Text"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
xcut
  • 6,219
  • 1
  • 33
  • 27
1

Some additional hint, if you have multiple elements linked and you want one bundle of elements or the other bundle, or both, you can do it like this:

<xsd:complexType name="ComplexTypeName">
    <xsd:choice>
        <xsd:sequence>
            <xsd:element name="theElement" />
            <xsd:element name="theElementIsFlagged" />
            <xsd:choice>
                <xsd:sequence>
<!-- note the empty sequence block -->
                </xsd:sequence>
                <xsd:sequence>
                    <xsd:element name="theOtherElement" />
                    <xsd:element name="theOtherElementIsFlagged" />
                </xsd:sequence>
            </xsd:choice>
        </xsd:sequence>
        <xsd:sequence>
            <xsd:element name="theOtherElement" />
            <xsd:element name="theOtherElementIsFlagged" />
        </xsd:sequence>
    </xsd:choice>
</xsd:complexType>

Just in case some of you bump into the same issue!!

krystine.e
  • 83
  • 7