XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
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) 2002-2013 HerongYang.com. 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 tutorial, we will write a new schema assigned to a different target namespace and try to import some declarations from xhtml.xsd.
Table of Contents
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
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
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
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