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

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:

  • Providing the schema location directly to the XML Schema processor.
  • Providing the schema location indirectly to the XML Schema processor by adding a special root attribute to the XML document.

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.

Sections in This Chapter

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

Dr. Herong Yang, updated in 2007
Assigning XML Schema Location in XML Documents