Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML Schema Complex Elements

XSD Complex Elements


A complex element contains other elements and/or attributes.


What is a Complex Element?

A complex element is an XML element that contains other elements and/or attributes.

There are four types of complex elements:

Note: All of the above elements can contain attributes!


Examples of Complex Elements

The complex element, "product", is empty:

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

  John

  Smith

The complex XML element, "food", contains only text:

Ice cream

The complex XML element, "description", contains elements and text:

It happened on 03.03.99 ....


How to Define a Complex Element?

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

  John

  Smith

In XML Schema, we have two ways to define a complex element:

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

  xs:complexType

    xs:sequence

     

     

   

 

If you use the method described above, then only "employee" can use the specified complex type. Note that its child elements, "firstname" and "lastname", are surrounded by the indicator . This means that the child elements must appear in the order in which they were declared. You will learn XSD Indicators more about indicators in this section.

  1. The "employee" element can use a type attribute, whose purpose is to refer to the name of the complex type to be used:

  xs:sequence

   

   

 

If you use the method described above, then several elements can use the same complex type, like this:

  xs:sequence

   

   

 

You can also base a complex element on an existing complex element and add some elements, like this:

  xs:sequence

   

   

 

  xs:complexContent

   

      xs:sequence