Skip to main content

Search 400 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, guides, blog and product.

XSD guide

XSD sequence vs choice

When child order is fixed, when alternatives are exclusive, and how minOccurs and maxOccurs change the meaning of an XSD compositor.

8 min read · Updated 2026-08-21

The short answer

Use xs:sequence when child elements must appear in the declared order. Use xs:choice when one of several alternatives may appear at that position. Put occurrence constraints on the compositor when the group itself repeats or is optional; constraints on a child govern only that child.

Sequence means ordered children

An xs:sequence is not merely a list of allowed names. It says firstName comes before lastName. Reversing two otherwise valid elements is a validation error.

Ordered person name
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="firstName" type="xs:string"/>
        <xs:element name="lastName" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Choice means alternatives at one position

An xs:choice with email and phone normally allows exactly one. It does not mean any number of either until maxOccurs says the choice group may repeat.

One contact method
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="contact">
    <xs:complexType>
      <xs:choice>
        <xs:element name="email" type="xs:string"/>
        <xs:element name="phone" type="xs:string"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
Valid email alternative
<contact><email>ada@example.com</email></contact>

Put cardinality at the right level

choice minOccurs="0" makes the whole alternative optional. choice maxOccurs="unbounded" allows a list containing either alternative at each position. A child maxOccurs changes only how that branch behaves once selected.

  • Optional group: <xs:choice minOccurs="0">
  • Repeating mixed alternatives: <xs:choice maxOccurs="unbounded">
  • Ordered optional child: place minOccurs="0" on that child inside xs:sequence.
  • Do not use xs:all as an unordered drop-in without checking its stricter occurrence rules.

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.