When you write HTML with some editors, you'll notice that there is this strange line written across the top<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">. While you may not know what it is, it still serves a purpose, and with XML that line of code is required to write a well-formed document.
This line is the "Document Type Declaration" that defines the "Dcoument Type Definition" or DTD. In the HTML declaration<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">there are four parts:
this tells the browser that this tag is a Document Type Declaration
the name of the DTD
the DTD is available publicly
- -//W3C//DTD HTML 4.0 Transitional//EN
the actual DTD used for the document, in this case HTML 4.0 Transitional in English
What is a DTD?
A DTD in an XML or HTML document provides a list of the elements, attributes, comments, notes, and entities contained in the document. It also indicates their relationship to one another within the document. In other words, a DTD is the grammar of an XML or HTML document.
Purpose of a DTD
When using a DTD for an XML document (including XHTML), the DTD is there to
provide structure for your documents. It is easy to write an XML document, but
without the DTD, it has no syntactic meaning to the computer. For example, if
you come across this portion of an XML document:<address>
<street>123 Any Street</street>
</address>While you would know it was an address, you wouldn't know whether it was an address the computer was supposed to create from a database table, if that address was for mailing, and other things. The computer, on the other hand, wouldn't see this as anything more than a string of text. If you viewed it in an XML browser, you might not get any errors, but you also wouldn't get a very interesting or useful page.
In other articles, we'll explore the inner workings of a DTD, and examine the elements that make up a DTD. You'll learn what an element, attribute, entity, and notation is and how to use them within your own DTDs. Plus, you'll learn how to read other DTDs so that you can use them in your own XML documents.