Herong's Tutorial Notes On XML Technologies
Dr. Herong Yang, Version 3.04

Document Type Definition (DTD)

This tutorial describes:

  • What is Document Type Definition (DTD)
  • XML Files Linked to External DTD Files
  • Validating XML Files Against DTD Statements

What is Document Type Definition (DTD)

DTD: A language that defines validation rules for XML files. DTD stands for Document Type Definition.

Main features of DTD:

  • DTD statements can be included inside XML files.
  • DTD statements can be stored as separate files and linked to XML files.
  • DTD offers validations rules on almost every aspects of XML files.

DTD Statements Inside XML Files

The easiest way to apply a set of validation rules written in DTD statements to an XML file is to include the DTD statements in the XML file.

Here is my "Hello world!" XML file with DTD statements, hello_int_dtd.xml:

<?xml version="1.0"?>
<!DOCTYPE p [
 <!ELEMENT p ANY>
]>
<p>Hello world!</p>

Note that:

  • A document type (DOCTYPE) declaration statement is added right bellow the "xml" process instruction.
  • The DOCTYPE statement specifies that "p" is the name of the root element.
  • The DOCTYPE statement specifies that all DTD statements are enclosed in this statement enclosed by "[" and "]".
  • Only one DTD statement used here, which says that the "p" element can have "ANY" thing as its body.

XML Files Linked to External DTD Files

DTD File: A text file that contains DTD statements representing a set of XML validation rules.

Instead of including the DTD statements inside an XML file, we can also store DTD statements in a DTD file, and link the DTD file to the XML file.

Here is a DTD file, hello.dtd, for my "hello.xml" file:

<?xml version="1.0"?>
<!ELEMENT p ANY>

Here is my "Hello world!" XML file linked to an external DTD file:

<?xml version="1.0"?>
<!DOCTYPE p SYSTEM "hello.dtd">
<p>Hello world!</p>

Note that:

  • The DOCTYPE statement contains a key word "SYSTEM" to indicate that the DTD statements are included in an external DTD, followed by the DTD file name.
  • The DTD file also contains the "xml" processing instruction as its first line.

Validating XML Files Against DTD Statements

Not all the XML file browsers support DTD validation. For example, IE 6.0 browse XML files every well as I mentioned in my previous notes, but I don't know how to make it to validate XML files against the supplied DTD statements.

XML DTD validation tools:

  • XML Spy 5.3
Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - Document Type Definition (DTD)