What is DTD in XML? Document Type Definition Explained
A Document Type Definition (DTD) is a set of markup declarations that defines the structure, legal elements, and attributes of an XML document. In XML processing, a DTD acts as a formal contract or schema that specifies the exact building blocks of an XML file, ensuring that the data is organized consistently and can be reliably shared between different applications. This guide explains what a DTD is, its core components, how it is implemented, and its role in XML validation.
Core Purpose of a DTD
The primary function of a DTD is to validate XML data. While any XML file must be “well-formed” (adhering to basic XML syntax rules like properly closed tags and correct nesting), a DTD determines whether the document is also “valid.” A valid XML document conforms to the specific rules and constraints laid out in its DTD, preventing errors during automated parsing and data exchange.
Key Components of a DTD
A DTD defines four primary types of components within an XML document:
- Elements (
<!ELEMENT>): Defines the allowed tags and their content models, such as whether an element can contain text, child elements, or be empty. - Attributes (
<!ATTLIST>): Specifies the attributes that belong to an element, including the attribute type (e.g.,CDATA,ID,IDREF), default values, and whether the attribute is required (#REQUIRED) or optional (#IMPLIED). - Entities (
<!ENTITY>): Defines reusable variables or text shortcuts (similar to macros) that can represent characters, phrases, or external files within the XML document. - Notations (
<!NOTATION>): Declares references to external non-XML data formats, such as image or media types, allowing the parser to handle them appropriately.
Types of DTD Declarations
A DTD can be defined directly inside an XML file or stored as a separate file and referenced remotely.
1. Internal DTD
An internal DTD is placed directly inside the XML document within the
<!DOCTYPE> declaration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Dev Team</to>
<from>Architect</from>
<heading>Reminder</heading>
<body>Review the XML structure by Friday.</body>
</note>2. External DTD
An external DTD is stored in an independent file (usually with a
.dtd extension) and referenced using the
SYSTEM or PUBLIC keyword. This approach
enables multiple XML documents to share the same structural rules.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Dev Team</to>
<from>Architect</from>
<heading>Reminder</heading>
<body>Review the XML structure by Friday.</body>
</note>Advantages of Using a DTD
- Standardization: Establishes a uniform format for data interchange across different software and organizations.
- Automated Validation: XML parsers can automatically check incoming documents for missing tags or invalid nesting before processing.
- Content Reuse: Entities allow developers to manage repeated text or global definitions from a single source.
Limitations of DTD
While DTD was the original schema language for XML, it has notable limitations:
- No XML Syntax: DTD uses its own non-XML syntax, requiring parsers to support two distinct grammars.
- Weak Data Typing: DTD treats almost all data as
simple text (
#PCDATA), offering no native support for data types such as integers, dates, booleans, or custom regular expressions. - No Namespace Support: DTD does not natively support XML namespaces, making it difficult to combine multiple vocabularies into a single document.
Due to these constraints, modern applications frequently use XML Schema Definition (XSD) or RELAX NG for advanced validation, though DTD remains widely supported and utilized in legacy systems, publishing pipelines (such as EPUB), and standardized document workflows.