TExcelTAD
Home › XML › XML DTD

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 · try it yourself
<?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
<?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 --valid to 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 · try it yourself
<?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 · try it yourself
<?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?

  1. Exactly one
  2. Zero or one
  3. One or more
  4. Zero or more

What does #REQUIRED mean in an ATTLIST?

  1. The attribute must be present
  2. The attribute has a default value
  3. The attribute must be unique
  4. The attribute is optional

Which of these can a DTD **not** express?

  1. The order of child elements
  2. That an attribute is one of a fixed list
  3. That an element's text must be a decimal number
  4. That an attribute is required