XML DTD
A DTD (Document Type Definition) describes the legal structure of a class of XML documents: which elements exist, what each may contain, and which attributes it has. It is the oldest XML validation language — it was inherited from SGML, XML's ancestor — and you'll still find it in many standards and older systems.
Internal and external DTDs
A DTD is attached through a <!DOCTYPE> declaration right after the XML declaration. It can be written inside the document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (to, from, subject?, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT subject (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ATTLIST note priority (low|normal|high) "normal">
]>
<note priority="high">
<to>Team</to>
<from>Priya</from>
<body>Stand-up moves to 10:30 tomorrow.</body>
</note>…or kept in a separate file that many documents share:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>…</note>Note: The browser parser in the playground checks well-formedness only — it reads the DTD but doesn't validate against it. Use a validating tool such as
xmllint --validto enforce the rules.
Declaring elements
<!ELEMENT name content-model>
| Content model | Meaning |
|---|---|
(#PCDATA) |
Text only ("parsed character data") |
EMPTY |
No content, e.g. <br/> |
ANY |
Anything at all (avoid — it disables checking) |
(a, b, c) |
Exactly these children, in this order |
(a | b) |
Either a or b |
(#PCDATA | em | strong)* |
Mixed content: text with em/strong mixed in |
Occurrence indicators follow a name or group:
| Indicator | Meaning |
|---|---|
| (none) | Exactly once |
? |
Zero or one (optional) |
* |
Zero or more |
+ |
One or more |
So <!ELEMENT recipe (title, ingredient+, step+, note*)> means: one title, at least one ingredient, at least one step, any number of notes — in that order.
Declaring attributes
<!ATTLIST element attribute type default>
| Type | Allowed values |
|---|---|
CDATA |
Any text |
(a|b|c) |
One of the listed values |
ID |
A unique identifier in the document |
IDREF / IDREFS |
The id(s) of other elements |
NMTOKEN |
A single name-like token (no spaces) |
| Default | Meaning |
|---|---|
#REQUIRED |
Must be present |
#IMPLIED |
Optional, no default |
"value" |
Optional; this value is used if missing |
#FIXED "value" |
If present, must equal this value |
<?xml version="1.0"?>
<!DOCTYPE library [
<!ELEMENT library (author*, book*)>
<!ELEMENT author (#PCDATA)>
<!ATTLIST author id ID #REQUIRED>
<!ELEMENT book (#PCDATA)>
<!ATTLIST book
by IDREF #REQUIRED
format (paper|ebook|audio) "paper"
isbn CDATA #IMPLIED>
]>
<library>
<author id="tp">Terry Pratchett</author>
<author id="nkj">N. K. Jemisin</author>
<book by="tp" isbn="978-0552134637">Guards! Guards!</book>
<book by="nkj" format="ebook">The Fifth Season</book>
</library>With a validating parser, by="xyz" would be rejected because no element has id="xyz", and format="vinyl" would be rejected because it isn't in the list.
Entities
A DTD can also declare entities — named pieces of text you can reuse with &name;:
<?xml version="1.0"?>
<!DOCTYPE letter [
<!ENTITY company "Northwind Trading Ltd.">
<!ENTITY sig "Kind regards, the &company; team">
]>
<letter>
<greeting>Dear customer,</greeting>
<body>Thank you for shopping with &company;.</body>
<closing>&sig;</closing>
</letter>Run it: the parser expands &company; and &sig; before reporting the text.
Warning: Entities can be abused. The "billion laughs" attack nests entities so that a tiny file expands to gigabytes, and external entities (
<!ENTITY x SYSTEM "file:///etc/passwd">) can leak server files — an attack called XXE. When parsing untrusted XML on a server, disable DTD processing or external entities.
DTD limitations
DTDs are compact and widely supported, but they:
- aren't written in XML syntax;
- have almost no data types — a
<price>can't be required to be a number; - don't understand namespaces.
XML Schema, covered next, fixes all three.
Test yourself
In <!ELEMENT order (customer, item+, note?)>, how many item elements are allowed?
- Exactly one
- Zero or one
- One or more
- Zero or more
What does #REQUIRED mean in an ATTLIST?
- The attribute must be present
- The attribute has a default value
- The attribute must be unique
- The attribute is optional
Which of these can a DTD **not** express?
- The order of child elements
- That an attribute is one of a fixed list
- That an element's text must be a decimal number
- That an attribute is required