Assigning XML Schema Location in XML Documents

This section describes a tutorial example on how to assign a schema location in an XML document without namespace using 'noNamespaceSchemaLocation' root attribute.

When you use an XML Schema processor to verify the validity an XML document against an XML Schema Definition (XSD) document, the location of the XSD document can be specified to the processor in two ways:

In many XML editors, adding the schema location attribute to an XML document is called "Assigning XML Schema", which requires 3 changes in the XML document:

1. 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.

2. If the XML document does not have its own namespace, specify the schema location with a special attribute on the root element: "xsi:noNamespaceSchemaLocation="URL-of-schema"".

Let's use our simplest XSD schema file, hello.xsd, to test this:

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

Here is an XML document, hello.xml, that conforms to this schema. But it does not have the schema location assigned in the XML document:

<?xml version="1.0"?>
<p>Hello world!</p>

Since there is no namespace used in the XML document, we can easily modify the XML document to include the schema location. The resulting XML document is hello_xsd_no_namespace.xml:

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

If the XML document has its own namespace, read other sections in this chapter on how to specify the schema location in the XML document.

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