XSD Composite Elements

Composite elements contain other elements and/or attributes.

What is a composite element?

A composite element refers to an XML element that contains other elements and/or attributes.

There are four types of composite elements:

  • Empty elements
  • Elements that contain other elements
  • Elements that only contain text
  • Elements that contain elements and text

Note:The above elements can all contain attributes!

Examples of composite elements

A composite element, "product", is empty:

<product pid="1345"/>

A composite element, "employee", only contains other elements:

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

A composite element, "food", only contains text:

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

A composite element, "description", contains elements and text:

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

How to define a composite element?

Please see this composite XML element, "employee", which only contains other elements:

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

In XML Schema, there are two ways to define a composite element:

1. By naming this element, you can directly declare the "employee" element, 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, only "employee" can use the specified composite type. Please note that its child elements, "firstname" and "lastname", are enclosed in the indicator <sequence>. This means that child elements must appear in the order they are declared. You will find XSD Indicators This section will learn more about indicators.

2. The "employee" element can use the type attribute, which is used to reference the name of the composite 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, you can use the same composite type for several elements, 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 add some elements on top of an existing composite element, based on some composite element, 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>