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

Importing Declarations Across Namespaces - "import" Component

This section describes how to import declarations from other schema documents under other namespaces using import components.

Earlier in this chapter, we learned how to use the "include" component to merge element declarations and type definitions from old schema documents into new schema documents.

The "include" component only works if both old and new schema documents are having no same namespace or having the same namespace.

If element declarations or type definitions needs merged or referenced across different namespaces, we need to use the "import" component, which has the following basic rules:

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

Rule 2. The "import" element must have the "namespace" attribute to specify the target namespace of the schema document to be imported.

Rule 3. The "import" element may have the "schemaLocation" attribute to specify where to fetch the schema document to be imported.

Rule 4. The imported namespace must be declared in the schema root with a prefix using "xmlns:xx="import-namespace"".

Rule 5. To use (or refer to) an element declaration from the imported schema, the "ref" attribute must be used on the element component.

In order to practice the "import" component, let's write a simple schema document, xhtml.xsd, assigned to the target namespace of "http://www.w3.org/1999/xhtml":

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

 <xsd:annotation>
  <xsd:documentation>xhtml.xsd
   Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
  </xsd:documentation>
 </xsd:annotation>

 <xsd:element name="p" type="xsd:anyType"/>
 <xsd:element name="pre" type="xsd:anyType"/>
 <xsd:element name="blockquote" type="xsd:anyType"/>
 <xsd:element name="ul" type="xsd:anyType"/>
 <xsd:element name="ol" type="xsd:anyType"/>

</xsd:schema>

Obvious, this is not a real schema document for the XHTML specification. But the real one will definitely have those elements declared.

In the next section, we will write a new schema assigned to a different target namespace and try to import some declarations from xhtml.xsd.

Sections in This Chapter

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

Dr. Herong Yang, updated in 2007
Importing Declarations Across Namespaces - "import" Component