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

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 provided 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.

Sections in This Chapter

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 again Schema Documents

Deriving New Simple Datatypes - "simpleType" Element

Defining Complex Datatypes - "complexType" Element

Validation Error Examples on Complex Datatypes

Dr. Herong Yang, updated in 2007
Declaring Root Elements - "element" Element