Three models for two children
START → a → b → ENDBoth, in that order.
↗ a ↘
START END
↘ b ↗One branch, once.
START → a → b → END
or → b → a → ENDBoth, either order.
These diagrams describe the specific declarations below: two required string elements, no nested groups and no repeated particles. The formal rules are in the W3C model-group specification.
Swap the children without changing their names
Select b → a. It fails under sequence, passes under all, and fails under this choice. The document is well-formed XML in every case; only its agreement with the schema changes.
Same XML. Different contract.
This sequence requires a, then b, exactly once each.
01 / Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="record">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>02 / XML instance
<record>
<a>value</a>
<b>value</b>
</record>Focused teaching model with complete downloadable examples. No arbitrary schema validation. The default example and comparison table remain readable without JavaScript.
Six inputs, three different accepted sets
| Children inside record | sequence | choice | all |
|---|---|---|---|
| a → b | Pass | Fail | Pass |
| b → a | Fail | Fail | Pass |
| a | Fail | Pass | Fail |
| b | Fail | Pass | Fail |
| a → a | Fail | Fail | Fail |
| (empty) | Fail | Fail | Fail |
Use the matrix during a schema review: point to the input you want to accept before discussing the syntax. A requirement such as “either contact method” needs different examples from “both fields, whichever comes first.”
The repetition trap
A choice with maxOccurs="unbounded" is a sequence of branch selections. If its alternatives are email and phone, it may accept email, email, email. That does not establish the requirement “one email and one phone, in either order.” Keep that counterexample in your fixture set.
Also distinguish repeating a group from repeating one child inside it. Repeating a sequence containing a then b permits a,b,a,b. Repeating each child inside one sequence instead can permit a,a,b,b. Draw the two paths on paper when reviewing a change; similar-looking indentation can conceal a different accepted language.
The diagrams here deliberately target XSD 1.0. In that version, an all-group has tighter placement and occurrence rules. XSD 1.1 changes some of those restrictions, so a design that works in one processor may be rejected by an older one. Consult the XSD 1.1 rules for a version-specific design.
Make the decision executable
Download the selected schema and instance, then run the pair through your validator:
xmllint --noout --schema compositors-example.xsd compositors-example.xml Keep at least one passing and one failing fixture for each requirement. Test duplicates and omissions explicitly. A single successful document tells you little about whether the grammar is too permissive.
For presence within the model, use the missing, empty and nil experiment. For reusing a model across several declarations, continue with global and local XSD design. The pattern analyzer can help inspect the larger structure.
Frequently asked questions
01What is the difference between xs:sequence, xs:choice and xs:all?
A sequence matches its particles in order. A choice selects a branch for each occurrence of the group. An all-group permits its children in any order, subject to the occurrence and version-specific restrictions.
02Does xs:choice always mean exactly one child element?
No. It chooses a branch. A branch may contain a group, and the choice can have its own occurrence constraints. Exactly one child is true for this example because the group occurs once and each branch is one required element.
03Can xs:all contain repeated elements?
In XSD 1.0, the child element particles of an all-group have maximum occurrence 1. XSD 1.1 relaxes some all-group restrictions. Check your validator version before adopting a design that depends on those changes.
04Are elements optional if minOccurs is omitted?
No. The default for minOccurs and maxOccurs is 1. The example therefore requires every element in its sequence or all-group once, and one selected branch in its choice.
05Is a repeated choice equivalent to an all-group?
No. Repetition of a choice can accept repeated occurrences of the same branch. An all-group with required children enforces the required members. Compare concrete input sequences before replacing one with the other.