DTD Elements and Attributes
Estimated study time: 16 minutes.
A DTD defines both the elements a document can use and the attributes those elements are allowed to carry. Each has its own declaration syntax.
Declaring Elements
<!ELEMENT course (title, duration, modules)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT duration (#PCDATA)>
<!ELEMENT modules (module+)>
<!ELEMENT module (#PCDATA)>
Common content model symbols:
#PCDATA— parsed character (text) data,— elements must appear in this exact order+— one or more occurrences*— zero or more occurrences?— optional, zero or one occurrence
Declaring Attributes
<!ATTLIST course
id CDATA #REQUIRED
level (beginner|intermediate|advanced) "beginner">
This says the course element must have an id attribute (required, free text), and may have a level attribute restricted to one of three values, defaulting to "beginner" if omitted.
Common Attribute Defaults
- #REQUIRED — the attribute must always be present.
- #IMPLIED — the attribute is optional.
- #FIXED "value" — the attribute always has this exact value.
- "default" — a specific default value used when the attribute is omitted.
level example above) whenever a field should only ever hold a fixed set of options — it catches typos at validation time instead of downstream.More Content Models: Choices, Mixed Content and EMPTY
Besides sequences and repeat markers, element declarations support a few other patterns you will meet often:
- Choice with
|:(email | phone)means exactly one of the two. - Grouping with parentheses:
(name, (email | phone)+)means a name followed by one or more contact items. - Mixed content:
(#PCDATA | em | strong)*allows text with inline tags in any order.#PCDATAmust come first and the whole group must end with*. - EMPTY: the element has no content, only attributes, like
<cover src="a.png"/>. - ANY: anything declared is allowed. It is handy while drafting but too loose for production.
Attribute Types Beyond CDATA
| Type | Meaning |
|---|---|
ID | A unique identifier across the whole document; must be a valid XML name, so it cannot start with a digit |
IDREF / IDREFS | One or several references to existing ID values |
NMTOKEN / NMTOKENS | A name-like token without spaces, or a space-separated list of them |
| Enumerated list | One value from a fixed set, such as (paper|ebook) |
A Complete Internal DTD to Validate
<?xml version="1.0"?>
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (cover?, title, author+, note?)>
<!ATTLIST book
code ID #REQUIRED
format (paper|ebook) "paper"
related IDREF #IMPLIED>
<!ELEMENT cover EMPTY>
<!ATTLIST cover src CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT note (#PCDATA | em)*>
<!ELEMENT em (#PCDATA)>
]>
<library>
<book code="b101" format="ebook">
<cover src="b101.png"/>
<title>Learning XML</title>
<author>A. Writer</author>
<note>Read the <em>second</em> chapter first.</note>
</book>
<book code="b102" related="b101">
<title>XML in Practice</title>
<author>B. Author</author>
<author>C. Author</author>
</book>
</library>
Save it as library.xml and check it with a validating parser. With libxml2 installed, run:
xmllint --noout --valid library.xml
xmllint --noout --dtdvalid library.dtd library.xml # for an external DTD file
No output means the document is valid. Now try breaking it on purpose to see what the validator reports: remove an author, put title after author, set format="pdf", use code="101", or point related at an ID that does not exist. Each change produces a separate error, which is a quick way to learn the rules.
Limits of DTDs and Safe Parsing
DTDs describe structure, but they cannot say that a value must be a number, a date or within a range. Everything is text. They also have weak support for namespaces. When you need data types or namespace-aware rules, XML Schema (XSD) is the better tool, while DTDs remain fine for simple, self-contained formats.
One security point matters whenever an application parses XML from users or other systems. DTDs can declare entities, including external ones that make the parser read files or URLs. To prevent XML external entity (XXE) issues, configure your parser to disable DTD processing, or at least external entity resolution, for untrusted input, and keep XML libraries up to date.