XML DTD – Define the Structure of XML Documents
Introduction – Why Learn XML DTD?
Before data can be trusted or processed, it must follow a consistent structure. That’s where XML DTD (Document Type Definition) comes in. DTD defines the legal structure, elements, and attributes of an XML document. It ensures that XML files are valid and predictable, especially in document-based and enterprise systems.
In this guide, you’ll learn:
- What XML DTD is and why it matters
- The difference between internal and external DTDs
- Syntax for defining elements, attributes, and nesting
- How to validate XML with DTD
What Is XML DTD?
DTD (Document Type Definition) is a set of rules used to define:
- Allowed elements and their order
- Attributes within elements
- How elements are nested and repeated
- Whether content is text, empty, or mixed
DTD helps XML parsers determine if the document is valid (not just well-formed).
Internal DTD Example
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to, from, message)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>
<note>
<to>John</to>
<from>Admin</from>
<message>Hello!</message>
</note>
<!DOCTYPE> declares the DTD inside the XML document.
External DTD Example
XML File:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>John</to>
<from>Admin</from>
<message>Hello!</message>
</note>
DTD File (note.dtd):
<!ELEMENT note (to, from, message)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT message (#PCDATA)>
External DTDs are reusable across multiple XML files.
Element Declaration Syntax
<!ELEMENT element-name content-type>
Content Types:
(#PCDATA)– Parsed Character Data (text)EMPTY– No contentANY– Any content(child1, child2)– Ordered elements(child1 | child2)– Choice (either-or)*– Zero or more+– One or more?– Zero or one
Example:
<!ELEMENT employee (name, position)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT position (#PCDATA)>
Attribute Declaration Syntax
<!ATTLIST element-name attribute-name type default>
Attribute Types:
CDATA– Any character dataID– Unique identifierIDREF– Reference to another ID(value1|value2)– Enumeration
Defaults:
#REQUIRED#IMPLIED#FIXED"defaultValue"
Example:
<!ATTLIST employee id ID #REQUIRED>
<!ATTLIST employee department (HR|IT|Sales) "IT">
Nesting and Repetition in DTD
<!ELEMENT company (employee*)>
<!ELEMENT employee (name, age?)>
*→ zero or more employees?→ age is optional
Validating XML with DTD
Use tools like:
xmllint:
xmllint --noout --dtdvalid note.dtd note.xml
- Online DTD Validators
- IDEs like Oxygen XML or XMLSpy
Limitations of DTD
| Limitation | Description |
|---|---|
| No data typing | Can’t enforce types like integers |
| No namespace support | Doesn’t support XML Namespaces |
| Limited extensibility | Can’t validate modern structured rules easily |
Use XSD (XML Schema) for more advanced validation.
Best Practices for DTD
- ✔️ Use external DTDs for reusability across projects
- ✔️ Always validate XML before deploying or parsing
- ✔️ Use naming conventions and comments in DTDs
- Don’t rely on DTDs for complex type validation—use XSD instead
Summary – Recap & Next Steps
DTD defines the structure and constraints of XML documents, ensuring consistency and validity. It’s an older but still useful schema language, especially for small or document-centric XML projects.
Key Takeaways:
- DTD ensures an XML document follows a defined structure
- Supports element nesting, attributes, optionality, and ordering
- Works with internal or external DTD declarations
Real-world relevance: Used in publishing systems, data interchange formats, and legacy enterprise applications that rely on document standards.
FAQs – XML DTD
What does DTD stand for in XML?
Document Type Definition. It defines the structure and legal elements of an XML file.
Can DTD validate attributes in XML?
Yes. You can define required attributes, default values, and data categories.
Which is better: DTD or XSD?
XSD is more powerful, supports namespaces and types. Use DTD for simpler needs.
What’s the difference between internal and external DTD?
Internal is embedded in XML; external is linked from a separate file.
Is DTD still used today?
Yes, but it’s mostly replaced by XML Schema (XSD) in complex systems.
Share Now :
