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

Declaring Root Elements - "element" Element

This section describes a tutorial example on how to use the Element Declaration Component to declare the root element for a conforming XML document.

We know that every XML document must have a root element. So the first thing we need to do in a schema is declare a root element for the conforming XML documents. This can be done by using an Element Declaration Schema Component, which is represented as an XML element named as "element" in schema XML representation document.

Rule 1. A schema must have at least one Element Declaration Component to declare a root element for the conforming XML document.

Rule 2. The XML representation of an Element Declaration Component is an "element" element, which must have one attribute called "name" to provide the element name for the conforming XML document.

Rule 3. The namespace of all elements used in the XML representation must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.

Here is a simple schema example represented as an XML document, word.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="word"/>
</xs:schema>

This is a very simple, but complete, schema represented as an XML document. It declares that the conforming XML document must have a root element called "word".

Here is an example XML document, word.xml, that conforms to the schema represented in word.xsd:

<?xml version="1.0"?>
<word>
extensible
</word>

Of course, schema, word.xsd, is too simple to be useful. The "word" element must be declared with additional properties to specify its content, attributes, and sub (child) elements. See the next section for more information.

By the way, there is nothing wrong to declare multiple root elements in a single schema document. The following example, word_term.xsd, declares two root elements, "word" and "term":

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="word"/>
 <xs:element name="term"/>
</xs:schema>

Any XML document with "word" or "term" as the root element is considered as conforming to word_term.xsd.

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

 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

 Complex Element Declaration

 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

Declaring Root Elements - "element" Element - Updated in 2014, by Dr. Herong Yang