A Document Type Definition contains the rules for a markup language, like HTML or XHTML (note that you can't create a DTD for XML, since that is a language to describe markup languages, like XHTML). Nowadays it is very important to point to a DTD at the start of your document (you could have an optional comment or processing instruction before it), since almost all online documents (HTML syntax or XHTML syntax) are send as
text/html (Internet Explorer doesn't support the correct content-type of XHTML; you could say that it doesn't support XHTML at all).
It is nessecary that your document validates, because of the different parsing modes in browsers. Valid HTML (or XHTML) is supposed to be handled exactly the same in every browser. Of course it isn't, but it is more accurate than the handling of tag soup (invalid HTML) across browsers. A DTD can help you validating your
text/html document, which brings it closer to become a cross-browser document.
Another reason is doctype switching, since modern browsers handle
text/html documents with a
DOCTYPE, different from those without. Adding
text/html to that sentence is important, since browsers in XML mode have only one rendering mode (the standard compliant one, best thing from
Hey Anne - nice write-up. Funny, after having sorted this out for myself I was just putting together a little something for a blog that I'm looking to get started this week.
Anyway, for what its worth, here's what I have (just the code so far) on serving up the right Content-type header and doctype with PHP.
I like the new weblog!
...browsers in XML mode have only one rendering mode (the standard compliant one ... in fact, iew uses quirks mode for XML served as XML (text/xml, application/xml), so that is not entirely true. For the moderns browsers it's true, however.
Also as important as the correct doctype is the correct mime type! I fell foul of this myself (and blogged about it).
This is especially important on dynamically generated sites using something like PHP. As you are probably aware, most webservers generaly squirt a text/html header out when it processes a PHP file. This is not good if your PHP is generating XHTML.
Another important reason for validating your (X)HTML can be that search engines prefer clear and valid code to tag soup. A minor syntax error can throw a spider off the track.