5.2. DTD Syntax

The most popular type of document model is the Document Type Definition (DTD). DTDs actually predate XML, being a hand-me-down from SGML with the syntax almost completely intact. A DTD defines a document type in the following way:

  • It declares a set of allowed elements. You cannot use any element names other than those in this set. Think of this as the "vocabulary" of the language.

  • It defines a content model for each element. The content model is a pattern that tells what elements or data can go inside an element, in what order, in what number, and whether they are required or optional. Think of this as the "grammar" of the language.

  • It declares a set of allowed attributes for each element. Each attribute declaration defines the name, datatype, default values (if any), and behavior (e.g., if it is required or optional) of the attribute.

  • It provides a variety of mechanisms to make managing the model easier, for example, the use of parameter entities and the ability to import pieces of the model from an external file. These mechanisms are described in Section 5.2.6 later in this chapter.

5.2.1. Document Prolog

Unlike an XML document, a DTD is not required to have a prolog. A DTD can have an optional XML declaration, the same kind as XML documents require. If you need to specify a character set other than the default UTF-8 (see Chapter 7, for more about character sets), or to change the XML version number from the default 1.0, this is where you would do it.

If you ...

Get Learning XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.