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

Importing Declarations Across Namespaces - Examples

This section describes a tutorial example on how to import external namespaces and refer to element declarations from imported namespaces.

A good example of using "import" components is to design a new schema that reuse (or refer to) elements declarations described in the "http://www.w3.org/1999/xhtml" name space.

In the following example, xhtml_import.xsd, declarations of "p", "pre", and "ul" are reused from the imported namespace:

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

 <xsd:annotation>
  <xsd:documentation>xhtml_import.xsd
   Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
  </xsd:documentation>
 </xsd:annotation>
 
 <xsd:import namespace="http://www.w3.org/1999/xhtml"
    schemaLocation="xhtml.xsd"/>

 <xsd:element name="book" type="bookType"/>
  
 <xsd:complexType name="bookType">
  <xsd:all>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="xsd:string"/>
   <xsd:element ref="xhtml:p"/>
   <xsd:element ref="xhtml:pre"/>
   <xsd:element ref="xhtml:ul"/>
  </xsd:all>
 </xsd:complexType>

</xsd:schema>

Notice that:

  • The "import" component specifies which namespace to import: namespace="http://www.w3.org/1999/xhtml".
  • The "import" component also specifies where to fetch the schema definitions for this namespace: schemaLocation="xhtml.xsd".
  • A prefix is declared for this namespace: xmlns:xhtml="http://www.w3.org/1999/xhtml".
  • The "ref="xhtml:p"" attribute is used to refer to the "p" element declaration from the imported name space.
  • There is no namespace assigned to the new schema.

In the next section, we will learn how to write an XML document that conforms to a schema that imports declarations from a different namespace.

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 - Examples