What is DTD in XML ?

DTD is a document-type definition. DTD contains a set of rules that control the structure and elements of XML files. When any XML file refers DTD file, it validates against those rules. DTD has validated elements and attributes that are defined inside the DTD document. It serves as a formal specification that outlines the elements, attributes, and their relationships within an XML document. They specify what elements can appear, their order, which attributes they can have, and what data types those attributes can contain.

Features of DTD:

  • Comprises structure, attributes, and elements of XML file
  • Validate XML files that help maintain data integrity and consistency.
  • Checks vocabulary of XML for grammatical rules
  • DTDs have a relatively simple and human-readable syntax
  • Allows to specify default values for attributes
  • Support mixed content models

Types of DTD: There are two ways to specify XML DTD.

  1. Internal DTD: Inside the document
  2. External DTD: Specified in a separate document and linked later


<!DOCTYPE element DTD identifier
declaration #1
declaration #2
declaration #n
] >

The start of the document has a doctype delimiter. DOCTYPE has two types of declarations:

  1. External subset declaration (optional)
  2. Internal subset declaration (optional)

While the internal declaration is specified in the document itself as a part of DOCTYPE, the external one is to be mentioned in a separate file. To refer to the external subset, public identifiers are used. An element instructs to parse the document from the root element. DTD identifier is the path to a system file or URL that points to any file on the internet. An optional list of entity declarations is enclosed inside square brackets ([]).


<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, genre)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT genre (#PCDATA)>

In this example, the DTD specifies that a “library” element must contain one or more “book” elements, each of which must contain “title,” “author,” and “genre” elements. PCDATA is parsed character data.

Advantages of using DTD:

  • Ensures consistency of XML data
  • Simple and easy to understand
  • Widely supported by XML parsers and applications
  • Easily interchange data between systems
  • Lightweight with limited file sizes and processing requirements.
  • Allows the definition of entity references for reusable content and markup

Disadvantages of using DTD:

  • DTDs lack built-in documentation mechanisms
  • Limited support for complex data types and advanced validation rules
  • DTDs do not provide native support for XML namespaces
  • More complex content models.
  • Do not offer built-in support for data types.
  • XML Schema Definition (XSD) replaced DTDs due to its more robust features and capabilities.

Where we can use the DTD:

  • XML documents: Most commonly used to define the structure and constraints of XML documents
  • Web Development: Web development to specify the structure of HTML (Versions 4.01 and earlier)
  • Configuration Files: Used to define the structure and rules for configuration file
  • RSS and Atom Feeds: Define the structure of RSS (Really Simple Syndication) and Atom feeds
  • Data Interchange Format : define the structure of XML based protocols of web services (SOAP) or data exchange formats like XML-RPC