Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this FAQ

The XML FAQ — Frequently-Asked Questions about the Extensible Markup Language

Section 3: Authors

Q 3.10: What's a DTD and where do I get one?

A specification of document structure. You can write one or download them.

A DTD is a description in XML Declaration Syntax of a particular type or class of document. It sets out what names are to be used for the different types of element, where they may occur, and how they all fit together. A Schema does the same thing in XML Document Syntax, so it can be read as an XML document itself; and Schemas allow more extensive data-typing.

For example, if you want a document type to be able to describe Lists which contain Items, the relevant part of your DTD might contain something like this:

 
<!ELEMENT List (Item)+> 
<!ELEMENT Item (#PCDATA)> 
	  

This defines a list as an element type containing one or more items (that's the plus sign); and it defines items as element types containing just plain text (Parsed Character Data or PCDATA). Validators read the DTD before they read your document so that they can identify where every element type ought to come, what they can contain, and how each relates to the other, so that applications which need to know this in advance (processors, browsers, editors, search engines, navigators, and databases) can set themselves up correctly. The example above lets you create lists like this:

<List>
  <Item>Chocolate</Item>
  <Item>Music</Item>
  <Item>Surfing</Item>
</List> 
	  

As explained in How does XML handle white-space in my documents?, the indentation in the example is just for legibility while editing: it is not required by XML. It could just as easily be written like this:

<List><Item>Chocolate</Item><Item>Music</Item><Item>Surfing</Item></List>
	  

A DTD therefore provides applications with advance notice of what names and structures can be used in a particular document type. Using a DTD and a validating editor means you can be certain that all documents of that particular type will be constructed and named in a consistent and conformant manner.

DTDs are not required for processing well-formed documents, but they are needed if you want to take advantage of XML's special attribute types like the built-in ID/IDREF cross-reference mechanism; or the use of default attribute values; or references to external non-XML files (‘Notations’) like images; or if you simply want a check on document validity before processing.

There are thousands of DTDs already in existence in all kinds of areas (see the SGML/XML Cover Pages for pointers). Many of them can be downloaded and used freely, but some are restricted to certain industries, or are proprietary; but you can also write your own (see the question on creating your own DTD. Old SGML DTDs need to be converted to XML for use with XML systems: read the question on converting SGML DTDs to XML; but most popular SGML DTDs are already available in XML form.

Some XML editors use a binary compiled format of DTD produced by their own management routines to allow a single person in an organisation to be in charge of modifications, and to distribute only an unmodifiable (binary compiled) version to users.

The alternatives to a DTD are various forms of Schema. These provide more extensive validation features than DTDs, including character data content validation.