XSD Composite Element

A composite element contains 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:All of the above elements can contain attributes!

Examples of composite elements

The composite element, "product", is empty:

<product pid="1345"/>

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

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

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

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

The 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 look at 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 the child elements must appear in the order they are declared. You will find XSD Indicator 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, several elements can use the same composite type, for example:

<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>