gtpx1m0eXML User's Guide

Validation

A valid document is one that follows the XML syntax and also conforms to the rules of an associated DTD or XML Schema. (A well-formed document is one that follows the XML syntax.) Both the DOM and SAX specifications enforce the rules for XML syntax whether you are using validation or not.

Validation is the process of comparing an XML document with a specified DTD or XML Schema. It ensures that the document uses only those tags that have been defined in the DTD or XML Schema as well as ensuring that it conforms to the element rules specified in the DTD or XML Schema. Both the DOM and SAX specifications have the ability to validate an XML document, but this is not a requirement for either.

When Should I Use Validation?

Validation of an XML document is expensive in terms of machine cycles. If the document is received from a reliable source and the format of the document has been predetermined, validation may not be necessary. However, using validation ensures that only elements defined in the DTD or XML Schema are used and, therefore, the structure of the XML document remains consistent.

If you do not want to validate the document each time you access data, you can, as an example, code an application so that it may reject tags that it does not recognize and takes an appropriate error path. If you do this, you may want to use validation during testing and initial implementation of a new version of an application or temporarily until the source of a document has been accredited.

How Do I Use Validation?

Validation is controlled by an API in the SAXParser and DOMParser classes. For more information about the classes for both DOM and SAX, go to XML4C Version 3.5.1 Documentation.

Results of Validation

The following table summarizes the expected results of validation:

Table 3. Summary of Expected Validation Results

  Validate Against a DTD Do Not Validate
Document is Valid Once validation is completed, parsing continues. Validation is ignored and processing continues.
Document is Not Valid Validation will result in an error response that will help you determine the error. Parsing is discontinued. Validation is ignored and processing continues.

For More Information

For more information, do the following: