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

Testing XML Schema Location with Namespaces

This section describes a tutorial example on how to validate an XML document with schema location assigned inside the XML document. Namespace is used in the schema and the XML document.

In this section, let's test those XML documents with schema location assigned and namespace specified. The tests were performed with XercesSchemaValidator.java described earlier in this chapter.

Test 1. Validating hello_xsd_default_namespace.xml passed ok:

>java -classpath .;\local\xerces-2_9_1\xercesImpl.jar 
   XercesSchemaValidator hello_xsd_default_namespace.xml

Parsing and validating: hello_xsd_default_namespace.xml

Passed.

Test 2. Validating hello_xsd_namespace_prefix.xml passed ok:

>java -classpath .;\local\xerces-2_9_1\xercesImpl.jar 
   XercesSchemaValidator hello_xsd_namespace_prefix.xml

Parsing and validating: hello_xsd_namespace_prefix.xml

Passed.

Test 3. If the namespace in the XML document does not match the target namespace in the XSD file, you will get following errors:

>type hello_xsd_namespace_wrong.xml

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


>java -classpath .;\local\xerces-2_9_1\xercesImpl.jar 
   XercesSchemaValidator hello_xsd_namespace_wrong.xml

Parsing and validating: hello_xsd_namespace_wrong.xml
Error:
   Line number: 3
   Column number: 42
   Message: TargetNamespace.1: Expecting namespace 
   'http://herongyang.com/x', but the target namespace of the 
   schema document is 'http://herongyang.com'.

Error:
   Line number: 4
   Column number: 67
   Message: cvc-elt.1: Cannot find the declaration of element 
   'hy:p'.

Failed with errors: 2

Conclusion: Namespace is an important factor when designing large XML schemas. The namespace must be specified both schema file and XML document with the same identification URI.

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
Testing XML Schema Location with Namespaces