Skip to content
Home » DTD-DOCUMENT TYPE DEFINITION

DTD-DOCUMENT TYPE DEFINITION


DTD – DOCUMENT TYPE DEFINITION

A Document Type Definition (DTD) is a set of rules that define the structure, elements, attributes, and legal building blocks of an XML document.
It ensures that an XML document follows a specific format and is valid according to defined constraints.

In other words, DTD acts as a blueprint or schema for XML data.

DTDs are used to:

✔ Validate XML documents
✔ Specify allowed tags and hierarchy
✔ Define the order and number of child elements
✔ Declare attributes of elements
✔ Maintain consistency across XML documents


WHY DO WE NEED DTD?

Without DTD, XML only checks whether the document is well-formed (correct tags).
With DTD, XML becomes valid, meaning the data structure follows defined rules.

✔ Ensures data accuracy

✔ Enforces structure

✔ Maintains interoperability

✔ Supports content validation

✔ Helps in data exchange across systems


TYPES OF DTD

XML supports two types of DTD:


⭐ 1. Internal DTD

DTD rules are written inside the XML file.

Example:

<?xml version="1.0"?>
<!DOCTYPE Student [
   <!ELEMENT Student (Name, Roll, Course)>
   <!ELEMENT Name (#PCDATA)>
   <!ELEMENT Roll (#PCDATA)>
   <!ELEMENT Course (#PCDATA)>
]>
<Student>
    <Name>Ravi</Name>
    <Roll>101</Roll>
    <Course>DBMS</Course>
</Student>

⭐ 2. External DTD

DTD is stored in a separate file (e.g., student.dtd), and XML references it.

student.xml:

<?xml version="1.0"?>
<!DOCTYPE Student SYSTEM "student.dtd">
<Student>
    <Name>Ravi</Name>
    <Roll>101</Roll>
    <Course>DBMS</Course>
</Student>

student.dtd:

<!ELEMENT Student (Name, Roll, Course)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Roll (#PCDATA)>
<!ELEMENT Course (#PCDATA)>

External DTD is useful for:

✔ Reusing the same structure
✔ Large enterprise systems
✔ Standardized document formats


DTD DECLARATIONS (ELEMENTS, ATTRIBUTES, ENTITIES)


1. ELEMENT DECLARATION

Used to define what elements exist and what they contain.

Syntax:

<!ELEMENT element-name content-model>

Content Models:

(#PCDATA) → parsed character data (text)
(child1, child2) → sequence
(child1 | child2) → choice
EMPTY → no content
ANY → any content allowed

Examples:

Text-only element:

<!ELEMENT Name (#PCDATA)>

Element with child elements:

<!ELEMENT Student (Name, Roll, Course)>

Choice:

<!ELEMENT Status (Pass | Fail)>

Zero or more occurrences:

<!ELEMENT Course (Subject*)>

One or more occurrences:

<!ELEMENT Items (Item+)>

2. ATTRIBUTE DECLARATION

Attributes add extra information to elements.

Syntax:

<!ATTLIST element-name attribute-name attribute-type default-value>

Attribute Types:

  • CDATA → text
  • ID → unique identifier
  • IDREF → reference to ID
  • IDREFS → multiple IDREF
  • ENUMERATION → fixed values
  • NMTOKEN → valid XML token

Examples:

<!ATTLIST Student roll CDATA #REQUIRED>
<!ATTLIST Book category (Science | Arts | Commerce) "Science">

3. ENTITY DECLARATION

Entities define reusable values.

Example:

<!ENTITY institute "ABC University">

Used inside XML as:

<College>&institute;</College>

4. NOTATION DECLARATION

Used to represent non-XML data (images, PDFs).

<!NOTATION jpg SYSTEM "image/jpeg">

ADVANTAGES OF DTD

✔ Simple and easy to learn
✔ Useful for document-centric XML
✔ Allows validation
✔ Supports reuse via external DTD
✔ No special tools required


DISADVANTAGES OF DTD

✘ No support for data types (only text)
✘ Cannot enforce number formats, dates, etc.
✘ No namespace support (limiting in modern XML)
✘ Less powerful than XML Schema (XSD)
✘ Syntax not XML-based (inconsistent)


WHEN TO USE DTD?

  • When document structure is simple
  • For legacy systems
  • When fast validation is required
  • When data types are not important
  • For widely shared document formats

Example:
HTML 4.01 uses DTD.


DTD vs XML Schema (quick comparison)

FeatureDTDXSD
SyntaxNon-XMLXML-based
Data TypesOnly textMany data types
NamespacesNoYes
ComplexitySimplePowerful
UseSimple documentsComplex enterprise XML

Perfect 5–6 Mark Short Answer

A Document Type Definition (DTD) defines the structure, elements, attributes, and valid content of an XML document. It ensures that the XML document is valid and follows a specific format. DTD can be internal or external and uses declarations like <!ELEMENT>, <!ATTLIST>, and <!ENTITY> to specify allowed elements and attributes. Although DTD is simple and useful for validation, it lacks data types, namespaces, and advanced features compared to XML Schema.