Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML Schema composite elements


May 28, 2021 XML Schema


Table of contents


XSD composite elements

Through this section, you'll learn about The composite elements of XML Schema, and in XML Schema, how composite elements should be defined.

Composite elements contain additional elements and/or properties.


What are composite elements?

A composite element is an XML element that contains other elements and/or properties.

There are four types of composite elements:

  • Empty element
  • An element that contains other elements
  • Contains only elements of text
  • An element that contains elements and text

Note: All of the above elements can contain properties!


An example of a composite element

The composite element, "product", is empty:

<product pid="1345"/>

Composite elements, "employee", contain only other elements:

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

Composite XML element, "food", contains only text:

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

Composite XML elements, "description" contains elements and text:

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


How do I define composite elements?

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

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

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

1. By naming this element, you can declare the "employee" element directly, as this is:

<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. N otice that its child elements, "firstname" and "lastname", are surrounded by indicators. T his means that child elements must appear in the order in which they are declared. You'll learn more about indicators in the XSD Indicators section.

2. The "employee" element can use the type property, which refers to the name of the compound 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, such as 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 build on an existing composite element and 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>

Here's what's going on with the XML Schema composite elements, and in the next section, we'll look at the composite empty elements.