XML DTD

A Document Type Definition (DTD) declares the legal building blocks of an XML document, letting a parser verify that elements, attributes, and their nesting match an agreed-upon contract.

What a DTD Does

A DTD defines which elements may appear in a document, what attributes each element can carry, how elements may be nested, and in what order children must appear. A document checked against a DTD and found compliant is called valid, which is a stronger guarantee than merely being well-formed.

DTDs can live inside the XML document itself (an internal subset) or in a separate .dtd file referenced from the document (an external subset). External DTDs are more reusable since many documents can share one definition.

Internal DTD Declaration

An internal DTD inside an XML document

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE note [
  &lt;!ELEMENT note (to, from, heading, body)&gt;
  &lt;!ELEMENT to (#PCDATA)&gt;
  &lt;!ELEMENT from (#PCDATA)&gt;
  &lt;!ELEMENT heading (#PCDATA)&gt;
  &lt;!ELEMENT body (#PCDATA)&gt;
]&gt;
&lt;note&gt;
  &lt;to&gt;Ravi&lt;/to&gt;
  &lt;from&gt;Meera&lt;/from&gt;
  &lt;heading&gt;Reminder&lt;/heading&gt;
  &lt;body&gt;Meeting moved to 3 PM.&lt;/body&gt;
&lt;/note&gt;
</pre>

</body>
</html>

External DTD Declaration

An external DTD is stored in its own file and referenced through a DOCTYPE declaration using SYSTEM (a private, local reference) or PUBLIC (a shared, registered reference).

Referencing an external DTD file

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE note SYSTEM "note.dtd"&gt;
&lt;note&gt;
  &lt;to&gt;Ravi&lt;/to&gt;
  &lt;from&gt;Meera&lt;/from&gt;
  &lt;heading&gt;Reminder&lt;/heading&gt;
  &lt;body&gt;Meeting moved to 3 PM.&lt;/body&gt;
&lt;/note&gt;
</pre>

</body>
</html>

Element and Attribute Declarations

Element content models use special symbols to express structure: a comma means a strict sequence, a pipe means a choice, a question mark means optional, an asterisk means zero-or-more, and a plus means one-or-more. Attributes are declared separately with ATTLIST, specifying a type and a default behavior such as #REQUIRED, #IMPLIED, or a fixed value.

Declaring attributes and repeatable children

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!ELEMENT library (book+)&gt;
&lt;!ELEMENT book (title, author, price?)&gt;
&lt;!ATTLIST book
  id CDATA #REQUIRED
  category (fiction | nonfiction | reference) "fiction"&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
&lt;!ELEMENT author (#PCDATA)&gt;
&lt;!ELEMENT price (#PCDATA)&gt;
</pre>

</body>
</html>
  • #PCDATA means the element may only contain parsed character text, no child elements
  • EMPTY means the element must have no content, like an XML self-closing tag
  • ANY means the element may contain any declared elements or text in any combination
  • #REQUIRED means the attribute must be present on every instance of the element
  • #IMPLIED means the attribute is optional and may be omitted entirely
Note: DTDs predate XML Namespaces, so they have no native concept of a namespace-qualified element name — this is one of the main reasons XML Schema was later developed.

Validating Against a DTD

Most full XML parsers (as opposed to lightweight browser parsers) support validating parsing, where the parser rejects a document that violates its DTD, reporting the specific rule that failed and where.

SymbolMeaningExample
,Sequence — items in this order(title, author)
|Choice — one of these items(fax | phone)
?Optional — zero or one(price?)
*Zero or more repetitions(note*)
+One or more repetitions(book+)
Note: DTDs use their own non-XML declaration syntax rather than XML elements, which makes them harder to edit with generic XML tools and is a common source of typos in ELEMENT and ATTLIST declarations.

Exercise: XML DTD

What is the primary function of a DTD in XML?