Complex Elements
A complex element is an XML element that contains other elements or attributes.
There are four types of complex elements: empty elements, elements that contain
other elements, elements that contain only text and attributes, and elements that
contain both text and other elements.
In the following XML code block <Tree>
is consider a complex
element because it contains other elements:
Trees.xml
<?xml version="1.0" ?>
<TreeInventory xsi:noNamespaceSchemaLocation="Trees.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Tree tid="1">
<ItemNumber>1047</ItemNumber>
<TreeName>
<CommonName>Colorado Blue Spruce</CommonName>
<ScientificName>Picea pungens</ScientificName>
</TreeName>
<Description>A magnificent sight of silver blue-green spruce.</Description>
<Price>1.99</Price>
<Quantity>50</Quantity>
<Type>Evergreen</Type>
<Picture>1047.jpg</Picture>
</Tree>
</TreeInventory>
Listing 3-17. Complex elements.
A complex element is declared using the <xsd:complexType>
designation. It is coded immediately below the element declaration. If a
complex element contains other elements, the <xsd:sequence>
is used to indicate that the parent element contains a sequence of child
elements.
Trees.xsd
<xsd:element name="Tree">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ItemNumber" type="xsd:integer"/>
<xsd:element name="TreeName" type="xsd:string"/>
..... other elements .....
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Listing 3-18. Complex element declaration.
The partial Schema above declares Tree
a complex element that
contains other elements. Its child elements are listed between the open
and closing <xsd:sequence>
block. Here
ItemNumber
and TreeName
are defined as
child elements of the Tree
element. In the XML file, the
sequence of child elements must appear in the order defined by the Schema.
Recall that an empty element that contains attributes is considered a complex
element. Suppose an XML empty element named Tree
is defined
and assigned an attribute tid
:
<Tree tid="1111"/>
Listing 3-19. Defining complex elements.
In the Schema below, we define the Tree
element followed by
the complexType
element. Since this element does not include
content, the Tree
element is included. The
complexContent
element signals that we intend to restrict
or extend the content model of a complex type, and the restriction of integer
declares one attribute but does not introduce any element content.
<xsd:element name="Tree">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:integer">
<xsd:attribute name="tid" type="xsd:integer"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
Listing 3-20. Restricting or extending complex type models.
An "elements-only" complex type contains an element that contains only other
elements. Below the <TreeName>
element contains two other
elements.
Trees.xml
<?xml version="1.0" ?>
<TreeName>
<CommonName>Colorado Blue Spruce</CommonName>
<ScientificName>Picea pungens</ScientificName>
</TreeName>
Listing 3-21. "Elements-only" complex type (XML).
The Schema is defined as follows:
Trees.xsd
<xsd:element name="TreeName">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CommonName" type="xsd:string"/>
<xsd:element name="ScientificName" type="xds:string"/>
</xsdsequence>
</xsd:complexType>
</xsd:element>
Listing 3-22. "Elements-only" complex type (XSD).
First the <TreeName>
element is defined followed by the
complexType
designation. Since the
<TreeName>
element contains
two other elements in sequence, they are defined within the
sequence
designation. In the XML file, all instances of the
<TreeName>
element must contain a common name and a
scientific name. Also, common name must always preceed scientific name.
In addition to complex elements that are empty elements, and elements that
contain other elements, some complex elements contain only text and an
attribute. Recall that elements that contain only text and no attributes
are considered Simple Elements.
<Tree tid="1111"/>White Oak</Tree>
Listing 3-23. Simple content.
This type contains only simple content (text and attributes), therefore we
add a simpleContent
element around the content. When using
simple content, you must define an extension or a restriction within the
simpleContent
element:
<xsd:element name="Tree">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="tid" type="xsd:integer"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
Listing 3-24. Defining extensions and restrictions within simple elements.
The last type of complex element is an element that contains both text and
other elements. This is also known as a mixed type. The example below uses
the fields of a tree order form to demonstrate a mixed type.
<TreeOrder>
You ordered a<Tree>Red Maple</Tree>.
The tree id is <tid>1222</tid>
The cost is <price>4.00</price>.
</TreeOrder>
Listing 3-25. Mixed complex element.
The Schema below is used to define the elements of the XML document:
<xsd:element name="TreeOrder">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="Tree" type="xsd:string"/>
<xsd:element name="tid" type="xsd:integer"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
</xds:complexType>
</xsd:element>
Listing 3-26. Defining XML document elements.
To enable character data to appear between the child-elements of the
TreeOrder
element, the mixed
attribute
must be set to "true". The <xsd:sequence>
defines the
elements that appear within the TreeOrder
(Tree, tid, price).
Again, when coded in the XML document, these child-elements must appear in the
correct order to ensure that the XML content is valid.