XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

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.

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

Complex Element Declaration

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 & Text Content

 Using Shorthand for "complexContent" with "restriction"

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Complex Element vs. Simple Element - Updated in 2014, by Dr. Herong Yang