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

Creating Schema Documents - "schema" Element

This section describes a tutorial example on how to write the root element of the XML representation of a schema.

Now we are ready to create schema XML documents by following XML Schema specifications.

Rule 1. A schema document must be created with "schema" as the root element.

This rule is easy to follow. Here is my first test of schema XML document, empty.xsd:

<?xml version="1.0"?>
<schema>
</schema>

Let's use XsdSchemaChecker.java described in the previous section to see if empty.xsd is a valid schema document:

>java XsdSchemaChecker empty.xsd
Error:
   Line number: 2
   Column number: 9
   Message: s4s-elt-schema-ns: The namespace of element 'schema' 
   must be from the schema namespace, 
   'http://www.w3.org/2001/XMLSchema'.

Schema File: empty.xsd
Parser Class: com.sun.org.apache.xerces.internal.jaxp.validation
  .SimpleXMLSchema

Failed with 1 errors.

Ok. We have our first syntax error which leads the second rule of schema documents.

Rule 2. The namespace of element 'schema' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.

There are two ways to meet this rule:

  • Set the default namespace with 'xmlns="http://www.w3.org/2001/XMLSchema"'
  • Set a namespace prefix with 'xmlns:xs="http://www.w3.org/2001/XMLSchema"', and use it for the root element 'schema' as 'xs:schema'. Of course, you can choose any string as the prefix. But "xs" and "xsd" are two commonly used prefix strings for the XML Schema namespace.

so we have two options to fix our first syntax error as shown below:

empty_default_namespace.xsd - Using the default namespace:

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

empty_xs_namespace.xsd - Using a namespace prefix:

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

Using a namespace prefix is the recommended option. Here is our schema check output on empty_xs_namespace.xsd:

>java XsdSchemaChecker empty_default_namespace.xsd

Schema File: empty_default_namespace.xsd
Parser Class: com.sun.org.apache.xerces.internal.jaxp.validation
   .SimpleXMLSchema

Passed.

Congratulations! We have successfully created our first valid schema document.

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
Creating Schema Documents - "schema" Element