XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
Defining Complex Datatypes - "complexType" Element
This section describes a tutorial example on how to define a complex datatype to be used to declare an element that can accept attributes and/or sub elements.
If element is declared with a simple datatype, for example, the "string" built-in datatype, the element must be "simple" - not allowed to have any attributes and any sub (child) elements.
If you want to declare an element to accept attributes or sub elements, you need to use the Complex Type Definition Component.
Rule 1. The Complex Type Definition Component allows you to define a new complex datatype, which can be used to declare elements to accept attributes and/or sub (child) elements.
Rule 2. The XML representation of a Complex Type Definition Component is a "complexType" element.
Rule 3. A "complexType" must have "name" attribute, if it is used to define a named datatype.
Rule 4. A simple way to specify an attribute in a "complexType" is to add an "attribute" component.
Rule 5. A simple way to specify a list of sub elements is to add a "sequence" element component.
Here is an example schema document, term.xsd, that declares element "term" to accept two sub (child) elements and one attribute:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- term.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <!-- Using a complex datatype --> <xs:element name="term" type="termType"/> <!-- Defining "termType" --> <xs:complexType name="termType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="definition" type="xs:string"/> </xs:sequence> <xs:attribute name="update" type="xs:date"/> </xs:complexType> </xs:schema>
The following XML document, term.xml, conforms to term.xsd:
<?xml version="1.0"?> <term update="2007-01-01"> <name>Extensible</name> <definition>Capable of being extended.</definition> </term>
Table of Contents
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
Schema and Schema XML Representation
Checking Schema Documents - XsdSchemaChecker.java
Creating Schema Documents - "schema" Element
Declaring Root Elements - "element" Element
Specifying Element Datatype - "type" Attribute
Using XML Schema Built-in Datatypes
Using XML Schema Built-in Datatypes Incorrectly
Validating XML Documents against Schema Documents
Deriving New Simple Datatypes - "simpleType" Element
►Defining Complex Datatypes - "complexType" Element
Validation Error Examples on Complex Datatypes
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
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