The Importance of Document Validation

Before digging deeper into DTDs, I'd like to address a topic that has been already been touched on a few times: document validation. I've already mentioned that the primary purpose of schemas is to provide a means of validating XML documents. What I didn't point out is that an XML document doesn't necessarily have to be valid in order to be useful. So, there is a little more to the document validation story than I've let on.

In addition to document validity, XML establishes the concept of a well-formed document, which is a document that meets all of the general language rules of XML. A well-formed document follows the five XML commandments you learned about in the previous tutorial, but it doesn't necessarily qualify as being a valid document. The reason is because a well-formed document doesn't have to be associated with a schema. Looking at XML documents in this light, it becomes apparent that there are two different degrees of "correctness" with respect to XML documents. The first degree is met by well-formed documents, which must meet the strict language requirements of XML. Taking things a step further leads us to valid documents, which are well-formed documents that also adhere to a schema of some sort (DTD or XSD, for example).

By the Way

A valid document is always a well-formed document, but the reverse is not always true.

The terms valid and well-formed are extremely important to XML. Whether or not a document needs to be valid is up to the particular application in which it is being used. However, all XML documents must be well-formed or you will undoubtedly encounter errors when the document is processed. To help keep valid and well-formed documents clear, remember that well-formed documents must adhere to the general XML rules, whereas valid documents must also adhere to a schema. You learn how to use DTDs and XSDs to validate XML documents in Tutorial 8.