Controlling Element Placement
You have already seen the xs:sequence
element, which dictates that the elements it contains
must appear in exactly the same order in which they appear within
the sequence element. In addition to xs:sequence
, schemas also provide the
xs:choice
and xs:all
elements to control the order in which elements may appear. These
elements may be nested to create sophisticated element
structures.
Expanding the form-letter example, a sequence adds support for
various letter components to the formletter.xsd
schema:
<xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="greeting"/> <xs:element name="body"/> <xs:element name="closing"/> </xs:sequence> </xs:complexType> </xs:element>
Now, thanks to the xs:sequence
element, a letter must include
a greeting
element, a body
element, and a closing
element, in that order. But, in
some cases, what is desired is that one and only one element appear
from a collection of possibilities. The xs:choice
element supports this. For
example, if the greeting
element
needed to be restricted to contain only one salutation out of a
permissible list, it could be declared to do so using xs:choice
:
<xs:element name="greeting"> <xs:complexType mixed="true"> <xs:choice> <xs:element name="hello"/> <xs:element name="hi"/> <xs:element name="dear"/> </xs:choice> </xs:complexType> </xs:element>
Now one of the permitted salutations must appear in the
greeting
element for the letter
to be considered valid.
The remaining ...
Get XML in a Nutshell, 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.