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:



  Sapna
  Tom
  Message
  Meeting on Monday at 11 AM.

A DTD File:

Note.dtd:






An XML Schema:

Note.xsd:





  
    
      
      
      
      
    
  



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:


Explanation:

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

Example 2:


  Tom
  Davis

Explanation:

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

Example 3:

Black

Explanation:

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

Example 4:


Today is an important day because today it is 16.11.19 ....

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:


  Tom
  Davis

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:


  
    
      
      
    
  

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:




  
    
    
  

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:






  
    
    
  

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




  
    
    
  



  
    
      
        
        
        
      
    
  

Please follow and like us:
Content Protection by DMCA.com