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:

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.

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