Unordered xsd:all elements followed by another element

238 views Asked by At

How can I write an XSD for the following XML?

<A></A>
<B></B>
<C></C>
<D></D>
<E></E>
<E></E>

A,B,C,D just have zero or one. And they don't have sequence. It could be D,C,B,A. And in the very end, there are one or more E element(s).

I have tried multiple ways, but can't get it done.

1

There are 1 answers

7
kjhughes On BEST ANSWER

You're blocked by a non-orthogonality of XML Schema. You'd think that the following should work:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="top">
    <xs:complexType>
      <xs:sequence>
        <xs:all>
          <xs:element name="A" minOccurs="0"/>
          <xs:element name="B" minOccurs="0"/>
          <xs:element name="C" minOccurs="0"/>
          <xs:element name="D" minOccurs="0"/>
        </xs:all>
        <xs:element name="E" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

However, it does not. XSD's xs:all construct is a second-class citizen; it cannot appear in a xs:sequence.

Recommendation: Drop the "any order" allowance from your requirements:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="top">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="A" minOccurs="0"/>
        <xs:element name="B" minOccurs="0"/>
        <xs:element name="C" minOccurs="0"/>
        <xs:element name="D" minOccurs="0"/>
        <xs:element name="E" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Allowing any order of elements ends up being less important in practice than in theory, especially given non-orthogonality such as the above and the potential for clashes with Unique Particle Attribution.