XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

Using Elements Declared in Different Namespaces

This section describes a tutorial example on how to use imported element declarations from a different namespace.

In the previous section, we wrote a new schema called xhtml_import.xsd that imported elements declarations from another namespace: xmlns:xhtml="http://www.w3.org/1999/xhtml". The final schema document represented a single schema with declarations from multiple namespaces:

  • The root "book" element is declared with no namespace.
  • Child elements "title" and "author" are declared with no namespace.
  • Child elements "p" "pre", and "ul" are declared in the namespace of http://www.w3.org/1999/xhtml.

When writing XML documents to conform to a schema with multiple namespaces, you need to remember the following rules:

  • A namespace prefix must be declared for each namespace involved in XML document.
  • The correct namespace prefix must be used for each element that is defined in a named namespace.

Here is a good example XML document, xhtml_import.xml, that conforms to xhtml_import.xsd:

<?xml version="1.0"?>
<book xmlns:xhtml="http://www.w3.org/1999/xhtml">
<!-- xhtml_import.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->

 <title>XML Schema Tutorials</title>
 <author>Herong Yang</author>
 
 <xhtml:p>What is XML Schema? ...</xhtml:p>
 <xhtml:pre>... schema code ...</xhtml:pre>

</book>

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

 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

 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

Using Elements Declared in Different Namespaces - Updated in 2014, by Dr. Herong Yang