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:
- null Element
- Elements that contain other elements
- Elements that contain only text
- Elements that contain elements and text
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:
The complex XML element, "food", contains only text:
The complex XML element, "description", contains elements and text:
It happened on
How to Define a Complex Element?
Look at this complex XML element, "employee", which contains only other elements:
In XML Schema, we have two ways to define a complex element:
- By naming the element, you can declare the "employee" element directly, like this:
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
- The "employee" element can use a type attribute, whose purpose is to refer to the name of the complex type to be used:
If you use the method described above, then several elements can use the same complex type, like this:
You can also base a complex element on an existing complex element and add some elements, like this: