In XML Schema, data units that are a collection of data fields are defined using complexType elements. Specifying a complex type requires three
pieces of information:
The name of the defined type is specified in the
nameattribute of thecomplexTypeelement.The first child element of the
complexTypedescribes the behavior of the structure’s fields when it is put on the wire. See Complex type varieties.Each of the fields of the defined structure are defined in
elementelements that are grandchildren of thecomplexTypeelement. See Defining the parts of a structure.
For example, the structure shown in Example 2.3 is be defined in XML Schema as a complex type with two elements.
Example 2.4 shows one possible XML Schema mapping for the structure shown in Example 2.3.
Example 2.4. A complex type
<complexType name="personalInfo"> <sequence> <element name="name" type="xsd:string" /> <element name="age" type="xsd:int" /> </sequence> </complexType>
XML Schema has three ways of describing how the fields of a complex type are organized
when represented as an XML document and passed on the wire. The first child element
of the complexType element determines which variety of complex
type is being used. Table 2.1 shows the elements used to define complex
type behavior.
Table 2.1. Complex type descriptor elements
If a sequence element, an all element, or a choice is not specified, then a sequence is assumed. For example, the structure defined in Example 2.4 generates a message containing two elements: name and age.
If the structure is defined using a choice element, as
shown in Example 2.5, it generates a message with either a name element or an age element.
Example 2.5. Simple complex choice type
<complexType name="personalInfo"> <choice> <element name="name" type="xsd:string"/> <element name="age" type="xsd:int"/> </choice> </complexType>
You define the data fields that make up a structure using element elements. Every complexType element should
contain at least one element element. Each element element in the complexType element represents
a field in the defined data structure.
To fully describe a field in a data
structure, element elements have two required attributes:
In addition to name and type, element elements have two other commonly used
optional attributes: minOcurrs and maxOccurs. These attributes place bounds on the number of times the field occurs
in the structure. By default, each field occurs only once in a complex type. Using these
attributes, you can change how many times a field must or can appear in a structure. For
example, you can define a field, previousJobs, that must occur at
least three times, and no more than seven times, as shown in Example 2.6.
Example 2.6. Simple complex type with occurrence constraints
<complexType name="personalInfo> <all> <element name="name" type="xsd:string"/> <element name="age" type="xsd:int"/> <element name="previousJobs" type="xsd:string: minOccurs="3" maxOccurs="7"/> </all> </complexType>
You can also use the minOccurs to make the
age field optional by setting the minOccurs to zero as shown in Example 2.7. In
this case age can be omitted and the data will still be
valid.
Example 2.7. Simple complex type with minOccurs set to zero
<complexType name="personalInfo> <choice> <element name="name" type="xsd:string"/> <element name="age" type="xsd:int" minOccurs="0"/> </choice> </complexType>
In XML documents attributes are contained in the element’s tag. For example, in the
complexType element name is an
attribute. They are specified using the attribute element. It
comes after the all, sequence, or
choice element and are a direct child of the complexType element. Example 2.8
shows a complex type with an attribute.
Example 2.8. Complex type with an attribute
<complexType name="personalInfo> <all> <element name="name" type="xsd:string"/> <element name="previousJobs" type="xsd:string" minOccurs="3" maxOccurs="7"/> </all> <attribute name="age" type="xsd:int" use="optional" /> </complexType>
The attribute element has three
attributes:
If you specify that the attribute is optional you can add the optional
attribute default. The default
attribute allows you to specify a default value for the
attribute.








