DTD
Stands for "Document Type Definition." A DTD defines the structure, elements, and attributes of an XML document. It provides a set of rules that XML files must follow, ensuring consistency and validity across similar documents.
A DTD specifies:
- What elements (tags) are allowed
- The order and nesting of elements
- What attributes elements can have
- The types of values those attributes may contain
Below is an example DTD for saving automobile data as XML:
<!DOCTYPE automobile [
<!ENTITY header "Car Details">
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ATTLIST model doors (two | four) #REQUIRED>
<!ELEMENT year (#PCDATA)>
<!ELEMENT engine (#PCDATA)>
<!ATTLIST engine transmission (manual | automatic) #REQUIRED>
]>
In the above DTD:
- An entity named header is defined with the value "Car Details."
- Elements like <make>, <model>, <year>, and <engine> are declared to contain parsed character data (#PCDATA), meaning they accept text.
- Attribute lists (ATTLIST) define valid attribute values. For instance, the model element must specify either "two" or "four" for the doors attribute, and the engine element must include a transmission attribute set to either "manual" or "automatic."
This automotive example is a relatively simple Document Type Definition. DTDs used in real-world applications — especially those that support large XML datasets — can span hundreds or thousands of lines. Small DTDs are often included inside XML documents, while large ones are saved as separate files (with a .DTD extension) and referenced via URL. Although newer schema languages like XML Schema (XSD) offer more flexible validation options, DTDs remain the standard way to define XML document structure.
NOTE: HTML 2 through 4.01 (1995-1999) used DTDs to define the allowed tags and attributes of an HTML file. HTML5 abandoned external DTD references and begins each HTML document with <DOCTYPE html>.