XML Schema (XSD): Using Compositors

In XML, you can use XSDs to define markup structures in varying levels of detail. Compositors are among the many XSD structures you can choose from when specifying your XML content models.

The XML Schema standard supports three different compositor types, each of which indicates restrictions on the range and ordering of elements. In general terms, a compositor dictates (or describes) the composition of child elements within a parent element in an XML data source.

Compositors often appear within XSD entries for complex type elements, providing specifications for elements with multiple possible children. They work in conjunction with “minOccurs” and “maxOccurs” attributes to define whether or not an element must appear, how many times it may appear and how it should appear in conjunction with its fellow elements in the same content model.

Sequence

The sequence compositor enforces a specific order on the child elements that appear within a parent element. The following XSD sample includes a sequence compositor:

<xs:element name="account">
<xs:complexType>
<xs:sequence>
<xs:element name="client_id" type="xs:integer"/>
<xs:element name="client_name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

This XSD excerpt specifies a parent element named “account” with two child elements named “client_id” and “client_name” which must appear in the same order. The following XML satisfies this XSD code:

<account>
<client_id>123</client_id>
<client_name>Lovely Products</client_name>
</account>

If the “account” element contained the two child elements, but not in this order, the XML would not satisfy the XSD, or, depending on your perspective, the XSD would not accurately describe the XML.

Choice

The choice compositor requires XML constructs to use only one from a possible set of child elements. The following XSD excerpt demonstrates:

<xs:element name="account">
<xs:complexType>
<xs:choice>
<xs:element name="corporate_id" type="xs:integer"/>
<xs:element name="personal_id" type="xs:integer"/>
</xs:choice>
</xs:complexType>
</xs:element>

This example could be for a service organisation with distinct corporate and personal accounts. Each account must be associated with either a corporate or personal ID number, but not both. The following XML satisfies the requirement:

<account>
<corporate_id>456</corporate_id>
</account>

Where the “minOccurs” and “maxOccurs” attributes allow XSDs to specify the number of times a single element can appear, the choice compositor defines which elements can appear with reference to other possible children within the same parent node.

All

The all compositor indicates that each member of a set of elements must appear, but only once, and in any order. The following XSD code includes an all compositor:

<xs:element name="account">
<xs:complexType>
<xs:all>
<xs:element name="client_id" type="xs:integer"/>
<xs:element name="client_name" type="xs:string"/>
<xs:element name="client_address" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>

This XSD section indicates three child elements to appear within the “account” parent element, but the child elements need not appear in the indicated order. The following XML code meets the requirement:

<account>
<client_address>5 High Street</client_address>
<client_id>123</client_id>
<client_name>Lovely Products</client_name>
</account>

This code contains one occurrence of each child element, but in an arbitrary order.

Nested Compositors

The elements nested within a compositor section of an XSD are referred to as particles. Compositors for sequence and choice can themselves include other compositors as particles. However, the all compositor has restrictions. Compositors of the all type cannot contain other compositors as children within an XSD, and each element within an all compositor has a maximum occurrence of one. This makes the all compositor relatively restrictive, so you are more likely to use and encounter the sequence and choice types.

Occurrences

As well as optionally containing further compositors, the sequence and choice compositors can include the full range of “minOccurs” and “maxOccurs” attribute values. It is possible to include these attributes with the all compositor, but the minimum can only be zero or one, and the maximum can only be one, so the attributes have limited value. The following XSD contains a nested compositor and elements with occurrence attributes:

<xs:element name="account">
<xs:complexType>
<xs:choice>
<xs:element name="corporate_id" type="xs:integer"/>
<xs:sequence>
<xs:element name="client_id" type="xs:integer"/>
<xs:element name="client_name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="client_address" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>

In this case, the “account” element may appear either with a “corporate_id” child element or with a sequence of “client_id”, “client_name” and “client_address” attributes. The “client_name” element does not need to appear, as its “minOccurs” attribute is set to zero. However, it can appear no more than once, with a single element at most representing the client name. The “client_address” element can appear multiple times, for example with each element containing a single line of the address in question. In this case, the sequence compositor appears as a particle of the choice compositor.

The following XML satisfies this XSD:

<account>
<client_id>123</client_id>
<client_name>Lovely Products</client_name>
<client_address>5 High Street</client_address>
<client_address>Metropolis</client_address>
</account>

Conclusion

As with any XSD structure, compositors must be chosen with a particular data set, and optionally application context, in mind. When used appropriately, these elements can help you to define robust, comprehensive data models.

Sue Smith works as a Web/ software developer and technical writer based in the UK. Sue has written for various clients including Smashing Magazine and Mobiletuts+. She also does a little Android development and some comedy writing. More articles by Sue Smith
Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress