An XML complex element contains other elements and/or attributes.
What is a Complex Element?
A complex element is an XML element that contains other elements and/or attributes.
Complex elements can be of four types:
- empty elements
- elements that contain only other elements
- elements that contain only text
- elements that contain both other elements and text
Examples of Complex Elements
A complex XML element, called "book":
<book author="Tom Nolan"/>A complex XML element, called "book", which contains only other elements:
<book>
<title>A Great Book</title>
<author>Tom Nolan</author>
<publisher>Tutorial Reference</publisher>
</book>A complex XML element, called "food", which contains only text:
<food type="dessert">Ice cream</food>A complex XML element, "description", which contains both elements and text:
<description>
I was born in <date lang="norwegian">01.02.99</date> ....
</description>How to Define a Complex Element
Consider the "book" XML element, which contains only other elements:
<book>
<title>A Great Book</title>
<author>Tom Nolan</author>
<publisher>Tutorial Reference</publisher>
</book>We can define a complex element in an XML Schema two different ways:
1. Declare directly by naming the element
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>In this way, only "book" element can use the specified complex type.
2. Declare with type attribute that referees to the name of the complex type to use
<xs:element name="book" type="bookinfo"/>
<xs:complexType name="bookinfo">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>In this way, you can have several element that can refer to the same complex type. For example:
<xs:element name="book" type="bookinfo"/>
<xs:element name="teacherbook" type="bookinfo"/>
<xs:element name="studentbook" type="bookinfo"/>
<xs:complexType name="bookinfo">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>Furthermore, you can base a complex type on an existing complex type and add some elements:
<xs:element name="book" type="enanchedbookinfo"/>
<xs:complexType name="bookinfo">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="enanchedbookinfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="publishdate" type="xs:date"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>