XML - Document Type Definition (DTD) in XML
Introduction
Document Type Definition, commonly known as DTD, is a mechanism used in XML to define the legal structure and content of an XML document. While XML itself provides rules for creating a well-formed document, DTD allows developers to specify additional rules about which elements and attributes are permitted, how elements should be arranged, and what types of values attributes can contain.
According to the W3C XML specification, a DTD provides a grammar for a class of XML documents. It can be included directly inside an XML document or stored separately and referenced from the document. A validating XML processor can then use these declarations to determine whether the document satisfies the defined validity constraints. (W3C)
DTD is particularly useful when multiple XML documents need to follow the same structural rules. For example, if an organization maintains hundreds of XML documents containing employee information, a DTD can ensure that every document follows a consistent structure.
Well-Formed XML vs Valid XML
Before understanding DTD, it is important to distinguish between well-formedness and validity.
A well-formed XML document follows the basic syntax rules of XML. For example:
<?xml version="1.0"?>
<employee>
<name>Rahul</name>
<department>IT</department>
</employee>
This document is well-formed because:
-
There is a single root element.
-
Every opening element has a corresponding closing element.
-
Elements are properly nested.
-
XML syntax is followed.
-
Attribute values, when present, are properly quoted.
However, being well-formed does not necessarily mean that the document follows a particular business structure.
A DTD can define additional rules such as:
-
An
employeemust containnameanddepartment. -
namemust occur beforedepartment. -
An employee must have an
idattribute. -
Certain attributes may be required or optional.
An XML document is considered valid when it satisfies the constraints defined by its associated DTD. (W3C)
What Is a DTD?
A DTD is a collection of declarations that describes the permitted structure of an XML document.
A typical DTD can define:
-
Element declarations
-
Attribute declarations
-
Entity declarations
-
Notation declarations
The XML specification identifies these as the main types of markup declarations that can occur in a DTD. (W3C)
For example:
<!ELEMENT employee (name, department)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>
This DTD states that an employee element must contain a name element followed by a department element.
Internal DTD
An internal DTD is written directly inside the XML document.
Example:
<?xml version="1.0"?>
<!DOCTYPE employee [
<!ELEMENT employee (name, department)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>
]>
<employee>
<name>Rahul</name>
<department>IT</department>
</employee>
The declarations between [ and ] form the internal DTD subset.
Here:
<!DOCTYPE employee [
declares that the document type is employee.
The following declarations define its structure:
<!ELEMENT employee (name, department)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>
The XML document therefore has a clearly defined structure.
The W3C specification permits the document type declaration to contain an internal subset, reference an external subset, or use both. (W3C)
External DTD
An external DTD is stored in a separate file. This approach is useful when many XML documents need to follow the same structure.
Suppose a file called employee.dtd contains:
<!ELEMENT employee (name, department)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>
The XML document can reference it:
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<name>Rahul</name>
<department>IT</department>
</employee>
The advantage is that the same DTD can be reused by multiple XML documents.
For example:
employee1.xml
employee2.xml
employee3.xml
employee4.xml
can all reference:
employee.dtd
This makes centralized structural control possible.
Element Declarations
Element declarations specify which elements are allowed and what their contents can contain.
The general form is:
<!ELEMENT element-name content-model>
For example:
<!ELEMENT employee (name, department)>
This means the employee element must contain name followed by department.
A DTD can define an element containing another element:
<!ELEMENT employee (name, address)>
<!ELEMENT address (city, state)>
The corresponding XML could be:
<employee>
<name>Rahul</name>
<address>
<city>Bengaluru</city>
<state>Karnataka</state>
</address>
</employee>
Common DTD Content Models
DTD provides several ways to describe element content.
Sequence
A comma specifies that elements must appear in a particular order.
<!ELEMENT employee (name, department, salary)>
The valid order is:
<employee>
<name>Rahul</name>
<department>IT</department>
<salary>50000</salary>
</employee>
Changing the order can make the document invalid according to the DTD.
Choice
The vertical bar | specifies alternatives.
<!ELEMENT payment (cash | card)>
This means the payment element can contain either cash or card.
For example:
<payment>
<card>12345</card>
</payment>
is permitted.
EMPTY
The EMPTY keyword indicates that an element cannot contain content.
<!ELEMENT image EMPTY>
The XML can use:
<image/>
but the element cannot contain text or child elements.
ANY
The ANY keyword allows an element to contain any content permitted by XML.
<!ELEMENT employee ANY>
Although ANY provides flexibility, it provides much less structural control than a specific content model.
#PCDATA
#PCDATA means parsed character data.
For example:
<!ELEMENT name (#PCDATA)>
allows text such as:
<name>Rahul</name>
The text inside the element is treated as character data.
Repetition Operators
DTD also provides operators for specifying how many times something may occur.
The ? operator indicates zero or one occurrence in content models where applicable.
The * operator means zero or more occurrences.
For example:
<!ELEMENT employee (phone*)>
allows an employee to have zero or more phone elements.
The + operator means one or more occurrences.
For example:
<!ELEMENT employee (phone+)>
requires at least one phone element.
These operators allow DTDs to describe variable-length structures.
Attribute Declarations
DTD can also define attributes associated with elements.
The general syntax is:
<!ATTLIST element-name attribute-name attribute-type default-value>
For example:
<!ATTLIST employee
id ID #REQUIRED
>
This specifies that every employee element must have an id attribute.
The XML could be:
<employee id="E101">
<name>Rahul</name>
<department>IT</department>
</employee>
The #REQUIRED keyword means that the attribute must be present. DTD also supports options such as #IMPLIED, default values, and #FIXED. (W3C)
Important DTD Attribute Types
DTD supports several attribute types.
CDATA
CDATA represents character data.
<!ATTLIST employee name CDATA #REQUIRED>
Example:
<employee name="Rahul">
ID
ID provides a unique identifier.
<!ATTLIST employee id ID #REQUIRED>
The ID value must uniquely identify an element within the XML document. (W3C)
IDREF
IDREF refers to the ID of another element.
This can be useful for representing relationships between XML elements.
Enumeration
An attribute can be restricted to a predefined set of values.
<!ATTLIST employee
type (permanent | temporary | contract) "permanent"
>
This means the type attribute can contain only one of the specified values.
Entity Declarations in DTD
DTD can define entities that allow reusable pieces of information.
For example:
<!ENTITY company "ABC Technologies">
The entity can then be referenced as:
<employee>
<company>&company;</company>
</employee>
The XML processor replaces the entity reference with its declared value.
DTD therefore provides a mechanism for defining reusable content and references. The XML specification also distinguishes between general entities and parameter entities. (W3C)
DTD Validation Example
Consider the following DTD:
<!DOCTYPE student [
<!ELEMENT student (name, course)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT course (#PCDATA)>
]>
A valid XML document would be:
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (name, course)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT course (#PCDATA)>
]>
<student>
<name>Anita</name>
<course>Computer Science</course>
</student>
The structure matches the DTD:
student
├── name
└── course
However, this document would violate the declared structure:
<student>
<course>Computer Science</course>
<name>Anita</name>
</student>
because the DTD requires name to occur before course.
Similarly, this would not satisfy the declared structure:
<student>
<name>Anita</name>
</student>
because course is required according to:
<!ELEMENT student (name, course)>
Advantages of DTD
DTD provides several useful capabilities:
1. Structural consistency
It ensures that XML documents follow a predefined structure.
2. Document validation
A validating XML processor can check whether the document complies with the DTD.
3. Reusability
An external DTD can be shared by many XML documents.
4. Attribute control
DTD can specify required, optional, fixed, and enumerated attribute values.
5. Entity support
DTD provides mechanisms for declaring and referencing entities.
6. Standardization
Organizations can establish a common structure for exchanging XML data.
Limitations of DTD
Despite its usefulness, DTD has several limitations.
Limited Data Types
DTD provides relatively limited attribute data types compared with XML Schema.
For example, DTD cannot directly define sophisticated types such as:
integer
decimal
date
time
boolean
with the same level of control available in XML Schema.
Limited Namespace Support
DTD was designed before XML namespaces became an important part of XML-based application design, making namespace handling less flexible than XML Schema.
Less Expressive Validation
DTD is good for describing basic document structure, but complex constraints can be difficult or impossible to express.
Separate Syntax
DTD uses a syntax different from normal XML syntax:
<!ELEMENT employee (name, department)>
whereas XML Schema itself is written using XML elements.
DTD vs XML Schema
DTD and XML Schema can both be used to describe and validate XML documents, but they have different capabilities.
| Feature | DTD | XML Schema |
|---|---|---|
| Basic XML structure | Yes | Yes |
| Element declarations | Yes | Yes |
| Attribute declarations | Yes | Yes |
| Rich data types | Limited | Extensive |
| Namespace support | Limited | Strong |
| Written in XML syntax | No | Yes |
| Complex validation | Limited | Strong |
| Reusable definitions | Yes | Yes |
| Entity declarations | Yes | No equivalent in the same DTD sense |
For simple XML structures, DTD can be sufficient. For modern applications requiring strong typing, namespaces, and sophisticated validation, XML Schema is generally more suitable.
DTD Processing
An XML processor reads the XML document and, when applicable, processes the associated DTD. The W3C specification distinguishes between validating and non-validating XML processors.
A validating processor checks the document against the constraints expressed in the DTD and reports validity violations. A non-validating processor is not required to perform the complete validity checking performed by a validating processor. (W3C)
This distinction is important when developing XML applications because simply attaching a DTD does not mean every XML parser will perform the same level of validation.
Practical Uses of DTD
DTD has historically been used in many areas where structured XML documents need consistent rules.
Examples include:
-
Document publishing systems
-
Technical documentation
-
Book and article structures
-
Data exchange
-
Configuration files
-
Legacy enterprise applications
-
XML-based content management systems
-
Standardized document formats
DTD remains important for understanding XML because it is part of the XML specification itself and provides foundational concepts such as element declarations, attribute declarations, entities, and document validation. (W3C)
Conclusion
Document Type Definition is a mechanism for defining the legal structure of an XML document. It can specify which elements may appear, their order, the type of content they can contain, the attributes associated with them, and reusable entities. DTDs can be placed inside an XML document as an internal subset or maintained separately as an external DTD.
The most important concept is the difference between well-formed XML and valid XML. Well-formedness ensures that basic XML syntax is correct, while DTD-based validation adds a defined structural contract that the document must follow. Although XML Schema offers more advanced validation capabilities today, understanding DTD remains essential for learning XML validation and for working with existing XML systems. (W3C)