XML Elements in Any Order, Some Required and Others Aren't

xml, xsd

Solution

`choice` only allows one of its child elements to be present in the XML graph. It looks like you want to use `sequence` if your elements are always in the same order. If the order is variable then you should use `all` and wrap all elements that have `maxOccurs="unbounded"` in a containing list element, because `all` only allows 1 or zero occurrences of its child elements.

EDIT:

And you should remove the minOccurs & maxOccurs from the choice element. This only allows you to enforce 3 choices, but doesn't allow you specify what choices they are (including repeating the same element multiple times). I don't know what exactly you're trying to enforce, but it won't be effectively enforced that way.

EDIT 2:

You can create list wrappers as follows (using the link element as an example):

<xs:element name="linkList" minOccurs="0" maxOccurs="1">
<xs:complexType>
  <xs:sequence>
    <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>
</xs:element>

and in the xml document you would (for example) nest your link elements in the linkList:

Old:

<other elements...>
<id>...</id>
<link>...</link>
<link>...</link>
<logo>...</logo>
<other elements...>

New:

<other elements...>
<id>...</id>
<linkList>
  <link>...</link>
  <link>...</link>
</linkList>
<logo>...</logo>
<other elements...>

Problem

I'm trying to have a list of elements that are allowed in any order. Some of the elements are required (min of 1, max of 1), some are optional with a maximum of one and some are optional with any number. This is what I have and the XSD is valid, but when I go to validate an XML, the rules that I'm trying to implement aren't enforced. For example, id is not made to be required. ``` <xsd:complexType name="feedType"> <xsd:annotation> <xsd:documentation> The Atom feed construct is defined in section 4.1.1 of the format spec. </xsd:documentation> </xsd:annotation> <xsd:choice minOccurs="3" maxOccurs="unbounded"> <xsd:element name="author" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="category" type="atom:categoryType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="contributor" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="generator" type="atom:generatorType" minOccurs="0" maxOccurs="1"/> <xsd:element name="icon" type="atom:iconType" minOccurs="0" maxOccurs="1"/> <xsd:element name="id" type="atom:idType" minOccurs="1" maxOccurs="1"/> <xsd:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="logo" type="atom:logoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="rights" type="atom:textType" minOccurs="0" maxOccurs="1"/> <xsd:element name="subtitle" type="atom:textType" minOccurs="0" maxOccurs="1"/> <xsd:element name="title" type="atom:textType" minOccurs="1" maxOccurs="1"/> <xsd:element name="updated" type="atom:dateTimeType" minOccurs="1" maxOccurs="1"/> <xsd:element name="entry" type="atom:entryType" minOccurs="0" maxOccurs="unbounded"/> <xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/> </xsd:choice> <xsd:attributeGroup ref="atom:commonAttributes"/> </xsd:complexType> ```

Original source