Documents Type Defination (DTD) Full details with suitable example
Introduction
- DTD (Document Type Definition) is a set of rules that define the structure, elements, and attributes of an XML or HTML document.
- It ensures that the document follows a specific format and maintains data consistency.
- In simple words, DTD defines what tags and attributes are allowed in a document and how they should be arranged.
- It acts as a blueprint for how the document should be written so that it remains valid, consistent, and machine-readable.
- It tells the XML parser what elements and attributes can appear, in what order, and what kind of data they contain.
Why DTD is Used
-
To define a consistent structure for XML documents.
-
To make XML documents self-descriptive and machine-verifiable.
-
To validate XML data before sharing or storing it.
-
To separate data content from data structure rules.
Purpose of DTD
- To define the legal structure of an XML or HTML document.
- To verify whether the document is well-formed and valid.
- To describe the relationships between different data elements.
- To ensure that data interchange between systems is consistent.
Types of DTD
There are two main types of DTD:
-
Internal DTD
-
External DTD
1. Internal DTD
- The DTD is written inside the same XML document.
- Declared within the
<!DOCTYPE>tag, inside the document.
Example (Internal DTD)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE student [
<!ELEMENT student (name, age, course)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT course (#PCDATA)>
]>
<student>
<name>Lav Pratap</name>
<age>23</age>
<course>MCA</course>
</student>
Explanation:
<!DOCTYPE student [...]>defines the root element.<!ELEMENT student (name, age, course)>means thestudentelement contains three child elements:name,age, andcourse.<!ELEMENT name (#PCDATA)>means#PCDATArepresents parsed character data (normal text).
2. External DTD
- The DTD rules are stored in a separate file (with
.dtdextension). - The XML document then links to that file.
Example (External DTD)
File 1: student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE student SYSTEM "student.dtd">
<student>
<name>Lav Pratap</name>
<age>23</age>
<course>MCA</course>
</student>
File 2: student.dtd
<!ELEMENT student (name, age, course)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT course (#PCDATA)>
Explanation:
SYSTEM "student.dtd"tells the XML parser to load structure rules from an external file namedstudent.dtd.- This allows the same DTD to be reused for multiple XML files.
DTD Syntax Elements
| Component | Description | Example |
|---|---|---|
<!ELEMENT> |
Defines an element | <!ELEMENT student (name, age, course)> |
<!ATTLIST> |
Defines an attribute of an element | <!ATTLIST student id ID #REQUIRED> |
#PCDATA |
Text data that will be parsed by the XML parser | <!ELEMENT name (#PCDATA)> |
EMPTY |
Element that cannot contain anything | <!ELEMENT image EMPTY> |
ANY |
Element can contain any data | <!ELEMENT info ANY> |
Attribute Declaration in DTD
Attributes of an element are defined using the <!ATTLIST> declaration.
Example
<!ELEMENT student (name, age, course)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT course (#PCDATA)>
<!ATTLIST student id ID #REQUIRED>
Explanation:
idis an attribute of the<student>element.IDspecifies that it must be unique.#REQUIREDmeans the attribute must be present.
Advantages of DTD
- Defines structure clearly and maintains uniformity.
- Validates documents to ensure correctness.
- External DTDs can be reused across multiple files.
- Useful for exchanging data between different systems.
Disadvantages of DTD
- Limited data types (supports only text-based validation).
- No namespace support.
- Complex for large documents.
- XML Schema (XSD) provides more advanced features.
Example of Complete XML with DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book id ID #REQUIRED>
]>
<library>
<book id="b1">
<title>Introduction to XML</title>
<author>Lav Pratap</author>
<price>250</price>
</book>
<book id="b2">
<title>HTML Basics</title>
<author>John Smith</author>
<price>200</price>
</book>
</library>
Summary
| Feature | Description |
|---|---|
| Full Form | Document Type Definition |
| Used In | XML / HTML |
| Purpose | Defines document structure and element rules |
| Types | Internal DTD and External DTD |
| File Extension | .dtd |
| Alternative | XML Schema (XSD) |
In short:
DTD defines the structure and rules of an XML or HTML document, ensuring that it is valid and well-formed.
Document Type Definition (DTD) defines the rules, elements, and attributes of an XML/HTML document.
It ensures that documents are well-structured, valid, and consistent according to a predefined model.

