5.1.8 XML Validation

All XML services have the ability to validate XML documents using DTD and XML Schema grammars.

All XML services use the SAX2 'org.xml.sax.XMLReader' interface and validation features are enabled via this interface.

All XML services use the 'org.apache.xerces.parsers.SAXParser' parser and have the following features enabled.

Feature

Description

http://xml.org/sax/features/validation

DTD validation is enabled.

http://xml.org/sax/features/namespaces

Name space processing is enabled.

http://apache.org/xml/features/validation/dynamic

Dynamic validation is enabled.
Validation is only done if the grammar is present in the document.

http://apache.org/xml/features/validation/schema

XML schema validation is disabled by default.
This feature can be enabled by the use of the SCHEMA keyword or 'validation.schema' property.

 

The DTD grammar is specified by the DOCTYPE before the root element.

 

<!DOCTYPE Orders SYSTEM "order.dtd">

 

The XML schema grammar is specified by the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute on the root element of the document.

The xsi prefix must be bound to the Schema document instance namespace, as specified by the recommendation.

Each document that uses XML Schema grammars must specify the location of the grammars it uses by using an xsi:schemaLocation attribute if they use namespaces, and an xsi:noNamespaceSchemaLocation attribute otherwise.

These are usually placed on the root / top-level element in the document, though they may occur on any element.

Here is an example with no target namespace:

 

<document xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:noNamespaceSchemaLocation='document.xsd'>
</document>

 

Here is an example with a target namespace. Note that it is an error to specify a different namespace than the target namespace defined in the Schema.

 

<document xmlns='http://my.com' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:schemaLocation='http://my.com document.xsd'>
</document>

 

What happens if both DTD validation and schema validation features are on ?

If both validators are present in the pipeline, then

If the instance document has only a DTD grammar (DOCTYPE before the root element), then only DTD validation errors are reported.

If the instance document has only XML Schema grammars, then only XML Schema validation errors are reported.

If the instance document has both DTD and XML Schema grammars, validation errors for both DTD and XML Schema are reported.

For more information refer to:

xml.apache.org/xerces2-j/faqs.html

xml.apache.org/xerces2-j/faq-pcfp.html

xml.apache.org/xerces2-j/features.html

xml.apache.org/xerces2-j/xml-schema.html