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

Including Old Schema Documents - "include" Component

This section describes why and how to include an existing schema document and reuse its datatype definitions.

When you are designing a new schema, you may want to reuse some declarations and definitions from an old schema. This can be done easily by using the "include" component to include the old schema document into the new schema document. To use the "include" component, you need to remember the following rules:

Rule 1. The XML representation of an "include" component is an "include" element.

Rule 2. The "include" element must have the "schemaLocation" attribute to specify where to fetch the old schema document.

Rule 3. The "include" element must appear before any element declarations or type definitions.

Below is a tutorial sample schema named address.xsd that declares an element "address" and a datatype "addressType":

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- address.xsd
 - Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
-->

 <xsd:element name="address" type="addressType"/>

 <xsd:complexType name="addressType">
  <xsd:sequence>
   <xsd:element name="street" type="xsd:string"/>
   <xsd:element name="city" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

The schema represented in address.xsd is a complete schema. The XML document, address.xml, conforms to this schema:

<?xml version="1.0"?>
<address>
<!-- address.xml
 - Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
-->

 <street>1 Main Street</street>
 <city>Vatican</city>

</address>

As you can see, the "addressType" defined in this schema is too simple. It is only good for a small country. In the next section, we will design a new schema to extend this definition for a bigger country. The new schema has an "include" component to reuse the "addressType" definition described in "address.xsd".

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

 XML Parser API - Xerces2 Java Parser

 XML Schema Language - Basics

 XML Schema Built-in Datatypes

 Complex Element Declaration

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

Linking Multiple Schema Documents Together

 Schema Component Reuse - "include", "redefine" or "import"

Including Old Schema Documents - "include" Component

 Including Old Schema Documents - Examples

 Including Old Schema Documents - Errors

 Redefining Old Datatypes - "redefine" Component

 Redefining Old Datatypes - XML Examples

 Redefining Old Datatypes - Errors

 Importing Declarations Across Namespaces - "import" Component

 Importing Declarations Across Namespaces - Examples

 Using Elements Declared in Different Namespaces

 Using Elements Declared in Different Namespaces - Errors

 Glossary

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2009
Including Old Schema Documents - "include" Component