When data moves between systems, a single misplaced tag or a missing field can break an entire process. XML solved part of this problem by giving data a structured, tag-based format. But XML on its own does not check whether the structure makes sense. That job belongs to a Document Type Definition, or DTD. A DTD acts as a rulebook that tells a parser exactly what an XML document is allowed to contain, in what order, and with what attributes. For anyone working with structured data, metadata standards, or digital catalogues, understanding DTDs is a foundational skill.
Table of Contents
What is a DTD?
A DTD is a set of declarations that define the building blocks of an XML document. It specifies which elements are allowed, how they relate to one another, what attributes they can carry, and what kind of data each element can hold. When an XML file references a DTD, a parser checks the file against these rules. If the file follows every rule, it is considered valid.
Think of a DTD as a blueprint. The DTD specifies rules that control the structure of any XML file that references it. It can declare elements, attributes, entities, and notations, and it sets constraints on how each of these can be used. A document that does not follow these declarations fails validation.
This matters most when data is shared across multiple applications or organisations. A library catalogue exchanged between two systems needs a guarantee that every record contains a title, an author, and a publication year in a predictable arrangement. The DTD provides that guarantee. Without it, each system would have to guess at the structure, and errors would multiply quickly.
Why DTDs matter for structured data
DTDs are widely used in publishing, documentation, and metadata environments because they enforce consistency. A DTD provides a grammar for classes of documents by describing possible elements, attributes, and their relationships. When hundreds of records must follow the same structure, this grammar prevents drift. One author cannot suddenly invent a new tag or reorder fields, because the parser will reject the document.
This is especially relevant for information professionals. Bibliographic standards, archival metadata, and content management systems often rely on defined document structures. A DTD turns an informal agreement about “how a record should look” into a machine-enforceable rule.
How a DTD is structured
A DTD is attached to an XML document through a DOCTYPE declaration. This declaration appears near the top of the document, right after the XML declaration line. The name in the DOCTYPE must match the root element of the document. The DOCTYPE informs the parser that a DTD is associated with the document, and the body of the DTD then declares elements, attributes, entities, and notations.
There are two ways to attach a DTD. An internal DTD is written inside the XML document itself, enclosed in square brackets within the DOCTYPE declaration. An external DTD is stored in a separate file with a .dtd extension and referenced from the XML document. External DTDs are preferred when the same structure is shared across many documents, since a single rulebook can govern an entire collection.
Declaring elements
Elements are the core of any DTD. The <!ELEMENT> declaration defines an element and states what it can contain. For a simple element holding text, the content type is written as #PCDATA, which stands for parsed character data. For an element that contains other elements, the child elements are listed inside parentheses.
Consider a small example. Suppose we want a note with four parts in a fixed order:
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
This DTD states that the root element note must contain exactly four children, in this exact sequence. The W3Schools reference shows that this DOCTYPE defines that the root element of the document is note and lists each permitted child. If a document places body before heading, or omits one of the four, it is invalid.
Special symbols control how often elements appear. A plus sign means an element must appear one or more times. An asterisk means zero or more times. A question mark means the element is optional and appears once or not at all. A comma separates a required sequence, while a vertical bar offers a choice between alternatives. These small symbols give a DTD considerable expressive power over structure.
Declaring attributes
Attributes attach additional information to an element. They are defined with the <!ATTLIST> declaration, which names the element, the attribute, the attribute type, and a default behaviour. A common pattern looks like this:
<!ELEMENT image EMPTY>
<!ATTLIST image height CDATA #REQUIRED>
<!ATTLIST image width CDATA #REQUIRED>
Here the image element carries two attributes, both holding character data, and both marked as required. The keyword #REQUIRED means the attribute must be present, while #IMPLIED would make it optional. This allows a DTD to enforce that every image record, for instance, always declares its dimensions.
Declaring entities
Entities are shortcuts. They let you define a piece of text once and reuse it throughout a document by reference. An internal entity is declared with the <!ENTITY> keyword and a value, then called using an ampersand and a semicolon. For example, declaring <!ENTITY hbd "happy birthday"> lets you write &hbd; anywhere in the document, and the parser expands it. Entities can also be external, pointing to a separate file through a SYSTEM or PUBLIC identifier, which is useful when the same content must be shared between multiple documents.
Creating a custom DTD
Designing a DTD begins with understanding the data you want to model. Suppose you are building a small catalogue of books. Each book has a title, an author, a year, and an ISBN. The first step is to identify the root element, then list its children and their content types.
An internal DTD for this catalogue might look like this:
<?xml version="1.0"?>
<!DOCTYPE catalogue [
<!ELEMENT catalogue (book+)>
<!ELEMENT book (title, author, year, isbn)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>
]>
The book+ notation means the catalogue must contain at least one book and may contain many. Each book must have all four children in the stated order. A parser reading this DTD will reject any book missing a title or carrying an unexpected tag.
For larger projects, the same declarations would be saved in a file such as catalogue.dtd and referenced externally. The XML file would then begin with <!DOCTYPE catalogue SYSTEM "catalogue.dtd">. This separation keeps the rulebook in one place, so updating the structure means editing a single file rather than every document.
A note on DTD syntax and limitations
DTDs use a syntax that is different from XML itself. This is one of their well-known drawbacks. Because the syntax differs from XML, the same tools used to edit and parse XML cannot directly process a DTD. DTDs also lack support for namespaces and cannot specify precise data types such as integers or dates. A year, for example, can only be declared as character data, not as a number with a range.
These limitations led to the development of richer alternatives like XML Schema Definition (XSD). Even so, DTDs remain in use because of their simplicity, concise syntax, and widespread tool support. Many existing systems and document standards were built on DTDs, so understanding them is essential for working with legacy data and for grasping how XML validation evolved.
DTD versus well-formed XML
One of the most important distinctions in XML is the difference between a well-formed document and a valid document. These terms are often confused, but they describe two separate levels of correctness.
What makes a document well-formed
A well-formed XML document follows the basic syntax rules of XML. It has a single root element, every opening tag has a matching closing tag, elements are properly nested without overlapping, attribute values are quoted, and tags are case-sensitive. A document that breaks any of these rules is not well-formed and cannot be processed at all. Importantly, a well-formed document does not need a DTD. It simply has to obey XML’s universal grammar.
What makes a document valid
Validity is a stricter requirement. A document is valid only when it is well-formed and also conforms to the rules of an associated DTD or schema. As one explanation puts it, a well-formed XML document adheres to XML syntax rules while a valid document also conforms to the constraints specified in its DTD. The DTD checks that the right elements appear in the right order with the right attributes.
The relationship between the two is one-directional. Every valid document must be well-formed, but a well-formed document is not necessarily valid. The property of validity implies well-formedness, but not the other way around. A useful way to remember this: a valid document is a stricter version of a well-formed one, much like how a square is always a rectangle but a rectangle is not always a square.
There is also a practical trade-off. Processing a merely well-formed document is faster because the parser does not need to check anything against a DTD. Validation adds an extra layer of verification, and if a valid document is transmitted to another system, the DTD may need to travel with it. For data that stays within a single, trusted application, well-formedness may be enough. For data shared across systems where structure must be guaranteed, validation against a DTD becomes valuable.
Where DTDs fit in practice
DTDs remain relevant in document publishing, technical documentation, and structured authoring environments. They are commonly used in systems that handle large volumes of standardised content, where every document must follow an identical pattern. For information science students, DTDs connect directly to the broader idea of controlled structure in metadata and digital records. A catalogue, an archive, or a digital repository all benefit from rules that prevent inconsistent or incomplete records.
The takeaway is straightforward. A DTD turns an XML document from something that is merely syntactically correct into something that is structurally trustworthy. It moves data from “this looks like XML” to “this follows our agreed rules.” That shift is what makes large-scale data exchange reliable.
What do you think? If you were designing a DTD for a digital library catalogue, which fields would you make mandatory and which would you leave optional, and why? And in your own work, when would the simplicity of a DTD outweigh the richer features offered by XML Schema?
References
- https://www.ibm.com/docs/en/radfws/10.0.0?topic=dtds-document-type-definitions-overview
- https://www.sciencedirect.com/topics/computer-science/document-type-definition
- https://www.tutorialspoint.com/dtd/dtd_syntax.htm
- https://www.w3schools.com/xml/xml_dtd.asp
- https://www.tutorialspoint.com/dtd/dtd_entities.htm
- https://www.tutorialspoint.com/xml/xml_dtds.htm
- https://en.wikipedia.org/wiki/Document_type_definition
- https://www.brainkart.com/article/Well-Formed-and-Valid-Documents_8650/

Leave a Reply