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.

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