Home Software DTD (Document Type Definition)

DTD in XML: Everything You Need to Know

What is a DTD (Document Type Definition)? How is a DTD used in XML? Learn all you need to know about DTD, its structure and use in XML validation. See an example of a DTD file and its use in XML.

By a TechBitBytes Contributor, August 11, 2023
6 MIN READ |

XML files lack instructions for their display. When opened in a browser, XML files are typically shown as raw data, often with modern browsers utilizing color-coded elements. An XML file containing the provided data will be displayed as presented in the browser.


<!-- Sample XML Syntax -->

<?xml version = "1.0" encoding = "UTF-8" ?>
<library>
	<book>
	  <title>Harry Potter and the Sorcerer's Stone</title>
	  <author>J.K. Rowling</author>
	  <genre>Fantasy</genre>
	  <published_year>1997</published_year>
	</book>
</library>

DTD (Document Type Definition)

DTD (Document Type Definition) is a markup language used to define the structure and rules for an XML document. It specifies the elements, attributes, and their relationships that are allowed in an XML document. DTD is a way to enforce a specific structure and ensure the validity of the XML content.

A DTD document consists of a collection of markup declarations that guarantee the validity and proper formatting of an XML document. These declarations establish the organization and arrangement of an XML document's elements. The DTD corresponding to the XML code provided is as follows:


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

We can save this file as

The DTD above defines the following rules;
defines the document type and includes the DTD rules.

defines that the root element is "library" and it must contain one or more "book" elements.

defines the "book" element, which must contain a "title" element, "author" element, an "genre" element and a "published_year" element.

defines the "title" element as containing parsed character data (#PCDATA).

defines the "author" element as containing parsed character data.

defines the "genre" element as containing parsed character data.

defines the "published_year" element as containing parsed character data.

The DTD can be added to the XML file as follow:

In this example, the declaration points to the DTD file ("library.dtd"), and the XML document follows the structure defined by the DTD.


<!-- Sample XML Syntax -->

<?xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
	.
	.
	.
	.
</library>


Considering to use a DTD?

XML does not require a DTD for it to be valid.

It is important to note that, for small projects, the DTD is not necessary.

DTDs provide basic structure validation but have some limitations, such as lack of support for data types, constraints, and more advanced validation. If you require more complex validation and want to take advantage of more powerful features, you might consider using other XML schema languages like XML Schema.

The XML schema is just an improved version of the DTD. Like the DTD, schema ensures that an XML document is both well formed and valid. Unlike DTD, XML schema has more features such as data types.

 

  This article is written to the best of the author's knowledge. TechBitBytes(TBB) ensures that all articles are constantly updated with the latest information.