What is DTD in XML?
Estimated study time: 14 minutes.
A DTD (Document Type Definition) defines the legal building blocks of an XML document — which elements can appear, in what order, and what attributes they may carry. It's essentially a contract that a document must follow to be considered valid, not just well-formed.
Well-Formed vs Valid
A document is well-formed if it follows XML's basic syntax rules. It's valid if it also conforms to a DTD (or schema) that defines its expected structure. A document can be well-formed without being valid, but not the other way around.
A Simple DTD
<!DOCTYPE course [
<!ELEMENT course (title, duration)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT duration (#PCDATA)>
]>
<course>
<title>Data Analytics</title>
<duration>4 Months</duration>
</course>
This DTD states that a course element must contain exactly a title followed by a duration, and both hold plain text (#PCDATA).
Why Use a DTD?
- Guarantees documents from different sources follow the same structure.
- Catches structural errors before data reaches downstream systems.
- Documents the expected shape of the data for other developers.
Content Models: Saying What an Element May Contain
The part in brackets after an element name is its content model. A few symbols control order and repetition:
| Syntax | Meaning |
|---|---|
(a, b) | a then b, in that order |
(a | b) | either a or b |
a? | a is optional, zero or one time |
a* | zero or more times |
a+ | one or more times |
EMPTY | no content at all, such as a line-break element |
ANY | any declared content; useful while drafting, too loose for real use |
A Fuller External DTD With Attributes and Entities
Real projects keep the DTD in its own file so many documents can share it. This one describes a library with books, attributes and a reusable entity.
<!-- library.dtd -->
<!ELEMENT library (book+)>
<!ELEMENT book (title, author+, price?, tag*)>
<!ATTLIST book
id ID #REQUIRED
format (print|ebook) "print"
language CDATA #IMPLIED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT tag (#PCDATA)>
<!ENTITY publisher "Open Shelf Press">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<book id="b101" format="ebook">
<title>Learning XML Step by Step</title>
<author>R. Sharma</author>
<tag>beginner</tag>
</book>
</library>
Reading the attribute declarations
- ID values must be unique across the document and must be valid XML names, so they cannot start with a digit.
- Enumerated types such as
(print|ebook)restrict values to a list, here withprintas the default. - #REQUIRED means the attribute must appear, #IMPLIED means it is optional with no default, and #FIXED forces one constant value.
- CDATA as an attribute type means any text.
The entity declared at the end can be used in text as &publisher;, and the parser replaces it with the full name. Change it once in the DTD and every document picks up the new value.
Checking Validity With xmllint
Most parsers only check well-formedness unless told to validate. The xmllint tool, part of libxml2, can validate against the DOCTYPE in one command:
xmllint --noout --valid library.xml
No output means the document is valid. Otherwise it prints the line and the rule that failed, for example a book missing its author, or an attribute value not in the allowed list.
DTD or XML Schema?
| Need | DTD | XSD |
|---|---|---|
| Simple structure rules | Yes | Yes |
| Data types like integer or date | No, text only | Yes |
| Namespaces | Poor support | Full support |
| Written in XML syntax | No | Yes |
| Entities | Yes | No |
Common DTD slips
- Declaring child elements in the wrong order, then writing documents in a different order.
- Forgetting to declare an element used in the document.
- Expecting
#PCDATAto enforce numbers; a price of "abc" is still valid. - Loading external DTDs from untrusted sources, which can expose parsers to entity expansion attacks. Disable external entity loading where it is not needed.