XSD Complex Elements

We can use the XML Schemas to define the elements of the XML files. An XML element that contains only text is called a simple element. No other elements or attributes can be included in it. However, other elements and/or attributes are included in a complex element.

Example:

Note.xml:

<?xml version="1.0"?>
<note>
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>

A DTD File:

Note.dtd:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

An XML Schema:

Note.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.codesjava.com"
xmlns="https://www.codesjava.com"
elementFormDefault="qualified">
 
<xs:element name="note">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="to" type="xs:string"></xs:element>
      <xs:element name="from" type="xs:string"></xs:element>
      <xs:element name="heading" type="xs:string"></xs:element>
      <xs:element name="body" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>
 
</xs:schema>

Explanation:

In the above example, we are using an XML Schema file to define the elements of the XML document “note.xml”. The note element contains other elements and is thus a complex type. The elements: to, from, heading, and body, do not contain other elements and are thus simple types.

Types of Complex Elements:

The complex elements can be of four types and each of these elements may contain attributes as well. Types of complex elements are:

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

Example 1:

<student marks="90"></student>

Explanation:

In the above example, we are defining a complex XML element called “student”, which is empty.

Example 2:

<student>
  <firstname>Tom</firstname>
  <lastname>Davis</lastname>
</student>

Explanation:

In the above example, we are defining a complex XML element called “student”, which contains only other elements.

Example 3:

<colour type="dark">Black
</colour>

Explanation:

In the above example, we are defining a complex XML element called “colour”, which contains only text.

Example 4:

<explanation>
Today is an important day because today it is <date lang="EN">16.11.19</date> ....
</explanation>

Explanation:

In the above example, we are defining a complex XML element called “explanation”, which contains both elements and text.

How to Define a Complex Element:

<student>
  <firstname>Tom</firstname>
  <lastname>Davis</lastname>
</student>

Explanation:

In the above example, we are defining a complex XML element called “student”, which contains only other elements.

How to define a complex element in an XML Schema?

There are two different ways to define a complex element in an XML Schema. These are:

Method 1:

We can directly declare the “student” element by naming the element.

Example:

<xs:element name="student">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"></xs:element>
      <xs:element name="lastname" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

Explanation:

In the above example, the specified complex type can be used only by the “employee” element. Also, the <sequence> indicator is surrounding the child elements, “firstname” and “lastname”, which means that the child elements must appear in the same order as they are declared.

Method 2:

A type attribute can be added to the “student” element to refer to the name of the complex type to use.

Example:

<xs:element name="student" type="studentinfo"></xs:element>
 
<xs:complextype name="studentinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"></xs:element>
    <xs:element name="lastname" type="xs:string"></xs:element>
  </xs:sequence>
</xs:complextype>

Explanation:

In the above example, we are using the second method and here, several elements can refer to the same complex type. The below example will explain it in a better way.

Example:

<xs:element name="undergraduate" type="studentinfo"></xs:element>
<xs:element name="postgraduate" type="studentinfo"></xs:element>
<xs:element name="school" type="studentinfo"></xs:element>
 
<xs:complextype name="studentinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"></xs:element>
    <xs:element name="lastname" type="xs:string"></xs:element>
  </xs:sequence>
</xs:complextype>

Example: To base a complex element on an existing complex element and add some elements:

<xs:element name="student" type="fullstudentinfo"></xs:element>
 
<xs:complextype name="studentinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"></xs:element>
    <xs:element name="lastname" type="xs:string"></xs:element>
  </xs:sequence>
</xs:complextype>
 
<xs:complextype name="fullstudentinfo">
  <xs:complexcontent>
    <xs:extension base="studentinfo">
      <xs:sequence>
        <xs:element name="mobile" type="xs:string"></xs:element>
        <xs:element name="state" type="xs:string"></xs:element>
        <xs:element name="country" type="xs:string"></xs:element>
      </xs:sequence>
    </xs:extension>
  </xs:complexcontent>
</xs:complextype>
Please Share