Assigning XML Schema Location with Namespaces

This section describes a tutorial example on how to assign schema location to a namespace used in the schema and the XML document.

If you decide to use namespace for your XML documents, then you need to respect the following rules:

1. The namespace must be specified in the XML Schema Definition (XSD) file using the "targetNamespace" attribute on the "schema" root element as "targetNamespace="namespace_uri"".

2. Declare a new namespace prefix in the XML document called, "xsi", with a special attribute on the root element: "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"". This is needed to provide the namespace prefix "xsi" for the next change.

3. The namespace must be specified together with the schema location in the "schemaLocation" attribute on the root element of the XML document as "xsi:schemaLocation="namespace_uri schema_url"".

4. If the namespace is used as the default namespace in the XML document, you can specify it with the "xmlns" attribute as "xmlns="namespace_uri"" on the root element.

5. If the namespace is used with a prefix in the XML document, you can specify it with the "xmlns:xx" attribute as "xmlns:xx="namespace_uri"" on the root element.

Now, let's add a namespace identified as "http://herongyang.com/xml" to hello.xsd and call it hello_namespace.xsd:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://herongyang.com">
 <xsd:element name="p" type="xsd:string"/>
</xsd:schema>

Then modify hello_xsd_no_namespace.xml to specify my namespace as the default namespace and call it hello_xsd_default_namespace.xml:

<?xml version="1.0"?>
<p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://herongyang.com" 
 xsi:schemaLocation="http://herongyang.com hello_namespace.xsd">
Hello world!</p>

In the second version, modify hello_xsd_no_namespace.xml to specify my namespace with a prefix "hy" and call it hello_xsd_namespace_prefix.xml:

<?xml version="1.0"?>
<hy:p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:hy="http://herongyang.com" 
 xsi:schemaLocation="http://herongyang.com hello_namespace.xsd">
Hello world!</hy:p>

Read the next section to see validation test results.

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

 Assigning XML Schema Location in XML Documents

 Validating XML Documents with Schema Locations

 Validating XML Documents with Schema Locations - JAXP

Assigning XML Schema Location with Namespaces

 Testing XML Schema Location with Namespaces

 Testing XML Schema Location with Namespaces - JAXP

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version