XML DTD Elements
Every element that appears in an XML file must be declared in the DTD if that file is to be valid. An element declaration tells the parser the name of the element and what content it is allowed to hold. This is the core of DTD — the element declaration.
The ELEMENT Declaration Syntax
<!ELEMENT element-name content-model>
The element-name is the tag name used in XML. The content-model describes what the element can contain.
Content Models Explained with Examples
1. Text-Only Elements (#PCDATA)
An element declared with #PCDATA holds only text. It cannot have child elements.
<!ELEMENT city (#PCDATA)>
Valid XML
<city>Mumbai</city>
Invalid XML
<city><name>Mumbai</name></city>
2. Child Elements (Sequence)
A comma-separated list means child elements must appear in that exact order.
<!ELEMENT person (firstName, lastName, email)>
Valid XML
<person> <firstName>Neha</firstName> <lastName>Kapoor</lastName> <email>neha@mail.com</email> </person>
Invalid XML — wrong order
<person> <lastName>Kapoor</lastName> <firstName>Neha</firstName> <email>neha@mail.com</email> </person>
3. Choice Between Elements
A pipe character | means the element can contain one of the listed options — but not more than one.
<!ELEMENT payment (creditCard | bankTransfer | cash)>
Valid XML
<payment><cash>500</cash></payment>
Invalid XML — two options used
<payment> <cash>200</cash> <creditCard>300</creditCard> </payment>
4. Occurrence Indicators
Occurrence indicators control how many times a child element appears.
| Indicator | Symbol | Meaning |
|---|---|---|
| Exactly once | (none) | Must appear exactly one time |
| Zero or more | * | Optional, can repeat |
| One or more | + | Required, can repeat |
| Zero or one | ? | Optional, appears at most once |
Examples
<!ELEMENT school (teacher+, student*, principal?)>
This means: a school must have one or more teachers, zero or more students, and optionally one principal.
5. Mixed Content
A mixed-content element can contain both text and child elements. Use #PCDATA with a pipe and star.
<!ELEMENT paragraph (#PCDATA | bold | italic)*>
Valid XML
<paragraph>This is <bold>important</bold> text.</paragraph>
6. EMPTY Elements
An EMPTY element has no content — no text, no children. It appears as a self-closing tag.
<!ELEMENT linebreak EMPTY>
Valid XML
<linebreak />
7. ANY Elements
ANY means the element can contain any declared element or text. This is rarely used because it removes all structure enforcement.
<!ELEMENT notes ANY>
A Complete DTD with Multiple Element Types
An inventory system for a hardware store:
<!DOCTYPE inventory [ <!ELEMENT inventory (category+)> <!ELEMENT category (categoryName, product*)> <!ELEMENT categoryName (#PCDATA)> <!ELEMENT product (productName, sku, price, inStock?)> <!ELEMENT productName (#PCDATA)> <!ELEMENT sku (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT inStock EMPTY> ]>
Valid XML for this DTD
<inventory>
<category>
<categoryName>Tools</categoryName>
<product>
<productName>Hammer</productName>
<sku>TL-001</sku>
<price>350</price>
<inStock />
</product>
<product>
<productName>Screwdriver</productName>
<sku>TL-002</sku>
<price>120</price>
</product>
</category>
</inventory>
Element Declaration Diagram
<!ELEMENT inventory (category+)>
| | |
| | +-- one or more category children
| +------------ element name is "inventory"
+------------------------ DTD element declaration keyword
<!ELEMENT product (productName, sku, price, inStock?)>
| | | |
| | | +-- optional child
| | +--------- required child
| +-------------- required child
+------------------------- required child
All in this exact order (sequence)
Common Mistakes in DTD Element Declarations
- Forgetting to declare all elements: Every element used in the XML must have a corresponding declaration in the DTD.
- Wrong sequence: Children in a sequence model must appear in the exact declared order.
- Mixing sequence and choice: (a, b | c) is ambiguous — use parentheses: (a, (b | c)).
- Using ANY too broadly: ANY removes all validation benefits for that element.
Key Points to Remember
- Every XML element used in a validated document must be declared in the DTD.
- #PCDATA elements hold text only — no child elements allowed.
- Comma separation means sequence — elements must appear in that order.
- Pipe | means choice — only one of the listed elements appears.
- *, +, and ? control how many times an element may occur.
- EMPTY elements have no content and appear as self-closing tags.
