An XML document consists of a prolog and a body. The document prolog contains the XML declaration and the document type declaration for that document, both of which are optional. The document type declaration specifies the root element of the document, and it can specify the DTD. The document body contains the actual marked-up document.
The markup in an XML document describes the document's storage and logical structure and associates attributes and their values with its logical structures, as described in Chapter 3. XML DTDs provide a vocabulary and syntax for describing a document's structure. The XML Recommendation provides document type declarations to define constraints on the logical structure and to support the use of predefined storage units.
The XML Recommendation [XML] defines the XML document type declaration as containing or pointing to markup declarations that provide a grammar for a class of documents. A markup declaration serves one of four purposes:
This grammar is known as a Document Type Definition. A DTD defines the allowable building blocks of an XML document; that is, it defines the document structure with a list of permissible elements, attributes, nestings, and so on.
The document type declaration can point to an external subset (a special kind of external entity) containing markup declarations, it can contain the markup declarations directly in an internal subset, or it can do both. The DTD for a document consists of both subsets taken together.
Because you can define a DTD within a document or reference and access it externally, a single DTD can apply to one document or to many documents. External DTDs, by convention, appear in an ASCII text file with the extension .dtd. For example:
Elements and attribute declarations form a framework against which a parser can test documents to see whether they meet the format described by the DTD. Declarations communicate information to the parser about document content, such as the following:
Although XML documents are not required to have a DTD, a large percentage of the XML specification [XML] deals with various sorts of declarations that are allowed in DTDs.
All XML documents must be well formed, as described in Chapter 3. A validating XML processor can use the DTD to validate a document that is, not only to require it to be well formed but also to determine whether the document conforms to the definitions. If the document can be parsed successfully with a validating parser and its DTD, the document is valid. To be valid, a document's DTD must specify all of its structure. XML documents read by a nonvalidating parser do not have to be valid.