Latest web development tutorials

XML Schema complex elements

Complex element contains other elements and / or attributes.


h2> What is a composite element?

Composite element refers to the XML element that contains other elements and / or attributes.

There are four types of composite elements:

  • Empty elements
  • It contains other elements of
  • Element contains only text
  • Element contains elements and text

Note: The above elements can contain attributes!


Examples of composite elements

Composite element, "product", is empty:

<product pid="1345"/>

Composite element, "employee", contains only other elements:

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>

Complex XML element, "food", containing only text:

<food type="dessert">Ice cream</food>

Complex XML element, "description" contains elements and text:

<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>


How to define complex element?

Look at this complex XML element, "employee", contains only other elements:

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>

In XML Schema, we have two ways to define complex elements:

1. Name the element directly to the "employee" element declaration, like this:

<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

If you use the method described above, the only "employee" can use the complex type specified. Please note that its child elements, "firstname" and "lastname", are surrounded by the indicator <sequence> in. This means that the child elements must appear in the order they are declared. You'll XSD Indicators learn more about the indicators in this section.

2. "employee" element can use the type attribute, the role of this attribute is a reference to the name of the complex type to be used:

<xs:element name="employee" type="personinfo"/>

<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

If you use the method described above, it can be used by a number of elements of the same complex type, like this:

<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>

<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

You can also over an existing composite element to a composite element basis, and then add some elements, like this:

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>