XSDComplex Elements
Complex elements contain 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:
- Empty elements
- Elements that contain other elements
- Elements that contain only text
- Elements that contain elements and text
Note: The above elements can all contain attributes!
Examples of Complex Elements
The complex element, "product", is empty:
The complex element, "employee", contains only other elements:
The complex XML element, "food", contains only text:
The complex XML element, "description" contains elements and text:
How to Define Complex Elements?
Consider this complex XML element, "employee", which contains only other elements:
In an XML Schema, we have two ways to define complex elements:
- By naming this element, you can directly declare the "employee" element like this:
If you use the method described above, only the "employee" can use the specified complex type. Note that its child elements, "firstname" and "lastname", are enclosed within the <sequence> indicator. This means the child elements must appear in the order they are declared. You will learn more about indicators in the XSD Indicators section.
- The "employee" element can use the type attribute, which references the name of the complex type to be used:
If you use the method described above, several elements can use the same complex type, like this:
You can also build upon an existing complex element by adding some elements, like this: