Compositors

The xs:sequence element is called a compositor, imposing order on its child xs:element particles. There are two other compositors available: xs:choice and xs:all. The xs:choice element permits one of a list of particles to appear, while xs:all requires all particles to appear but doesn’t put constraints on the order in which they appear. In addition to setting rules for their particles, compositors also act as a group, and you can specify minOccurs or maxOccurs for the group as a whole. (The default value for both minOccurs and maxOccurs is 1.)

If you wanted to define a person element that included both name and nationality but weren’t concerned about the order in which they appeared, you could use:

<xs:element name="person">
    <xs:complexType>
      <xs:all>
        <xs:element ref="name"/>
        <xs:element ref="nationality"/>
      </xs:all>
      <xs:attribute ref="id" use="required"/>
    </xs:complexType>
  </xs:element>

Tip

Notice that the xs:attribute isn’t part of the group. Attributes are part of the type, but the compositors apply only to element content.

If, on the other hand, you wanted to define a person element that could contain your choice of a name or alias, you might use:

<xs:element name="person">
    <xs:complexType>
      <xs:choice minOccurs="0" >
        <xs:element ref="name" />
        <xs:element ref="alias" />
      </xs:choice>
      <xs:attribute ref="id" use="required"/>
    </xs:complexType>
  </xs:element>

The particles inside an xs:sequence or xs:choice may be xs:element, xs:sequence, xs:choice, xs:any, or xs:group elements. ...

Get XML Pocket Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.