1. Home
  2. Computing & Technology
  3. Web Design / HTML

What is a DTD
Part 1: Document Type Definitions and Valid XML

By , About.com Guide

As you learned in another article, a DTD is the grammar of an XML page. It is an acronym that stands for Document Type Definition. It contains the elements, attributes, entities, and notations used in the XML document.

Valid XML

XML is really a markup language to create other markup languages, and the DTD is the tool you use to create and describe your language. Once a DTD is created and a document written based on that DTD, the document is then compared to the DTD. This is called validation. If your XML document follows the rules listed in the DTD, then the document is said to be valid. XML documents that don't follow the rules in their DTDs are called invalid.

For example, while we haven't gone over the structure of a DTD yet, here is part of a simple one. It states that there is a root element called "family" that has two possible elements within it: "parent" and "child":

<!DOCTYPE family [
  <!ELEMENT parent (#PCDATA)>
  <!ELEMENT child (#PCDATA)>
]>

If you were to write an XML document based upon that DTD, you could write:

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE family [
  <!ELEMENT parent (#PCDATA)>
  <!ELEMENT child (#PCDATA)>
]>

<family>
<parent>Judy</parent>
<parent>Layard</parent>
<child>Jennifer</child>
<child>Brendan</child>
</family>

This would be a valid XML document. But if I added extra text outside of the <parent> or <child> tags, the document would be invalid until I changed the DTD:

...
<family>
This is my family:
<parent>Judy</parent>
<parent>Layard</parent>
<child>Jennifer</child>
<child>Brendan</child>
</family>

Next Page > Elements, Entities, Attributes, and Notations > Page 1, 2, 3

Previous Features

Explore Web Design / HTML
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. XML
  5. DTDs
  6. What is a DTD>

©2009 About.com, a part of The New York Times Company.

All rights reserved.