Tangled web



Sunday, December 31, 2006

A List Apart: Articles: Learn to Read a Document Type Definition
 
A List Apart: Articles: How to Read W3C Specs

You know those <!DOCTYPE ...> declarations that you put in your documents to tell the browser which version of HTML or XHTML you're using? Those declarations refer to a Document Type Definition, or DTD, which defines which combinations of elements are legal in a document.

While learning to read a DTD is difficult, it's not an impossible task. And it's worth learning, because the DTD is the ultimate authority for what is and is not syntactically correct for a particular markup language.

A full explanation of how to read a DTD is well beyond the scope of this article, but it can be found in Elizabeth Castro’s XML for the World Wide Web Visual Quickstart Guide, or in Erik Ray’s Learning XML. Here's a brief example of something you might see in a DTD:

<!ENTITY %fontstyle '(tt | i | b)'>
<!ENTITY %inline '(#PCDATA | %fontstyle;)'>
<!ELEMENT div (p | %inline;)+>
<!ATTLIST div align (left | right | center) #IMPLIED>

And here’s what it means in English:

The font style elements are <tt>, <i>, and <b>. Inline elements consist of text or font style elements. A <div> can contain one or more <p> or inline elements in any order. A <div> has an optional align attribute with values of left, right, or center."