XML Schema Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

Complex Element vs. Simple Element

This section describes what is a complex element and how to declare different types of complex elements.

What is a simple element? A simple element is an XML element that does not have any attributes or any sub (child) elements. A simple element can be declared with a simple datatype.

What is a complex element? A complex element is an XML element that have at least one attribute, or at least one sub (child) element. A simple element can be declared with a complex datatype.

Rule 1. A complex element must be declared by an Element Declaration Component with a complex datatype.

Rule 2. A complex datatype must be defined by a Complex Type Definition Component.

Rule 3. The XML representation of a Complex Type Definition Component is a "complexType" element.

Rule 4. A "complexType" element must have "name" attribute, if it is used to define a named datatype.

Rule 5. A "complexType" element may have the following possible sub elements:

  • simpleContent - The declared element has simple content with no sub (child) element. But it must have one or more attributes.
  • complexContent - The declared element has complex content with sub (child) element. It may nor may not have any text content. And it may or may not have any attributes.

Combine with what learn from other chapters in this book, now we know that there are 7 different types of elements with different ways to declare them:

  • Simple type and no text content - Element with no text content, no sub (child) element, and no attribute. This is really an empty element, like the <br> element in a HTML document. An empty element can be declared with a "simleType" datatype derived from "string" with length of "0".
  • Simple type with text content - Element with text content, no sub (child) element, and no attribute. This type of element can be declared with a built-in datatype or a "simleType" derived from a built-in datatype.
  • Complex type with text content and attributes - Element with text content and attributes, but no sub (child) element. This type of element can be declared with a "complexType" datatype with "simpleContent" containing some "attribute"s.
  • Complex type with attributes - Element with one or more attribute, but no text content and no sub (child) element. This type of element can be declared with a "complexType" datatype with "complexContent" containing some "attribute"s.
  • Complex type with sub elements - Element with sub (child) elements, but no text content and no attribute. This type of element can be declared with a "complexType" datatype with "complexContent" containing some "element"s.
  • Complex type with sub elements and attributes - Element with sub (child) elements and attributes, but no text content. This type of element can be declared with a "complexType" datatype with "complexContent" containing some "element"s and some "attribute"s.
  • Complex type with sub elements, attributes and text content - Element with sub (child) elements, attributes, and text content. This type of element can be declared with a "complexType" datatype with "complexContent mixed="true"" containing some "element"s and some "attribute"s.

Tutorial examples will be provided on how to declare element for each of the 7 types listed above in other sections in this chapter.

Sections in This Chapter

Complex Element vs. Simple Element

Declaring Empty Elements

Declaring Simple Elements

Declaring Complex Elements with Simple Content

Declaring Complex Elements with Attributes Only

Declaring Complex Elements with Sub Elements Only

Declaring Complex Elements with Attributes and Sub Elements

Declaring Complex Elements with Attributes, Sub Elements and Text Content

Using Shorthand for "complexContent" with "restriction"

Dr. Herong Yang, updated in 2007
Complex Element vs. Simple Element