Open In App

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:

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

Syntax:

<!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 ([]).

Example:

<!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:

Disadvantages of using DTD:

Where we can use the DTD:

Article Tags :