2.7 Building Content ModelsBuilding Content Models
The definitions of complex types in the purchase order
schema all declare sequences of elements that must appear
in the instance document. The occurrence of individual
elements declared in the so-called content models of these
types may be optional, as indicated by a 0 value for the
attribute
minOccurs (e.g. in comment), or be
otherwise constrained depending upon the values of minOccurs and
maxOccurs. XML
Schema also provides constraints that apply to groups of
elements appearing in a content model. These constraints
mirror those available in XML 1.0 plus some additional
constraints. Note that the constraints do not apply to
attributes.
XML Schema enables groups of elements to be defined and
named, so that the elements can be used to build up the
content models of complex types (thus mimicking common
usage of parameter entities in XML 1.0). Un-named groups of
elements can also be defined, and along with elements in
named groups, they can be constrained to appear in the same
order (sequence) as they are declared. Alternatively, they
can be constrained so that only one of the elements may
appear in an instance.
ref17To illustrate, we introduce two groups
into the PurchaseOrderType definition from the
purchase order schema so that purchase orders may contain
either separate shipping and billing addresses, or a single
address for those cases in which the shippee and billee are
co-located:
NOTE:
Nested Choice and Sequence Groups
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:choice>
<xsd:group ref="shipAndBill"/>
<xsd:element name="singleUSAddress" type="USAddress"/>
</xsd:choice>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:group id="shipAndBill">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
</xsd:sequence>
</xsd:group>
The choice
group element allows only one of its children to appear in
an instance. One child is an inner group element that references
the named group shipAndBill consisting of the
element sequence shipTo, billTo,
and the second child is a singleUSAddress.
Hence, in an instance document, the
purchaseOrder element must contain either a
shipTo element followed by a billTo
element or a singleUSAddress element. The
choice group is
followed by the comment and items
element declarations, and both the choice group and the element
declarations are children of a sequence group. The effect
of these various groups is that the address element(s) must
be followed by comment and items
elements in that order.
ref18There exists a third option for
constraining elements in a group: All the elements in the
group may appear once or not at all, and they may appear in
any order. The all
group (which provides a simplified version of the SGML
&-Connector) is limited to the top-level of any content
model. Moreover, the group's children must all be
individual elements (no groups), and no element in the
content model may appear more than once, i.e. the
permissible values of
minOccurs and maxOccurs are 0 and 1.
For example, to allow the child elements of
purchaseOrder to appear in any order, we could
redefine PurchaseOrderType as:
NOTE:
An 'All' Group
<xsd:complexType name="PurchaseOrderType">
<xsd:all>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:all>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
By this definition, a comment element may
optionally appear within purchaseOrder, and it
may appear before or after any shipTo,
billTo and items elements, but it can
appear only once. Moreover, the stipulations of an all group do not allow us to
declare an element such as comment outside the
group as a means of enabling it to appear more than once.
XML Schema stipulates that an
all group must appear as the sole child at the
top of a content model. In other words, the following is
illegal:
NOTE:
Illegal Example with an 'All' Group
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:all>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element name="items" type="Items"/>
</xsd:all>
<xsd:sequence>
<xsd:element ref="comment" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
Finally, named and un-named groups that appear in content
models (represented by
group and
choice,
sequence,
all respectively) may carry minOccurs and maxOccurs
attributes. By combining and nesting the various groups
provided by XML Schema, and by setting the values of
minOccurs and
maxOccurs,
it is possible to represent any content model expressible
with an XML 1.0 DTD. Furthermore, the all group provides additional
expressive power.
|