Farewell, XML declaration

For years I have been using an XML declaration prior to my DOCTYPE declaration:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/ TR/xhtml1/DTD/ xhtml1-strict.dtd">

It was obvious for me that such a declaration would be a good thing to have since XHTML is XML after all. Only later I learned that it triggers a bug in IE6 to render pages in quirks mode with a broken box model. But I was used to build workarounds for Internet Explorer bugs since IE 3.0 and never knew anything but the broken box model in IE, so I continued to use the declaration.

Until recently my colleague Thomas Junghans took the trouble to research, and found this phrase in the XHTML specification:

An XML declaration is not required in all XML documents; however XHTML document authors are strongly encouraged to use XML declarations in all their documents. Such a declaration is required when the character encoding of the document is other than the default UTF-8 or UTF-16 and no encoding was determined by a higher-level protocol.

The declaration is not required as long as you encode in UTF-8 or UTF-16, you are only strongly encouraged to use it. Gasp!

Richard Ishida lists some advantages of using the declaration when serving the pages with an XML MIME type or viewing them offline, however I’m convinced the disadvantages prevail.

So as long we are stuck with IE6 — and people say that could be as long as 2014 when support for Windows XP ends (reminder to self: throw a party on 8 April, 2014) — I will refrain from using the XML declaration. Amen.

5 Responses to ‘Farewell, XML declaration’

  1. Dirk Ginader

    I am so going to party with you in 2014 ;-)

  2. Richard Ishida

    Well, actually I point out pros and cons, but I usually recommend to omit the declaration and use UTF-8. (And that’s what I do for my own pages.)

    It used to be a problem, however, when I used XMetal as my XHTML editor, since it requires the XML declaration to load the file correctly. So I had a little script that stripped off the XML declaration before uploading to the server, then replaced it.

    Now I author pretty much all my XHTML in DreamWeaver, and don’t need the scripts.

  3. Martin Kliehm

    Richard, I’m glad you clarified it and generally agree with me. In the referred document you recommend to omit the XML declaration to avoid quirks mode in IE6, but to use it always when a page is served as application/xhtml+xml. There are more disadvantages in serving a page as application/xhtml+xml, but I suppose that’s a subject for another post, like “The Great XHTML Shootout.”

    By the way, after I had been using Homesite for a long time (that doesn’t support UTF-8 well) I switched to Aptana a couple of years ago. It’s the same as Eclipse, but less resource-hungry, less Java IDE, but with more built-in front-end coding support. Also the integration of Subversive is so stable and convenient!

  4. John A. Bilicki III

    My site is strictly standards compliant (XHTML 1.1 as application/xhtml+xml with 1.0 Strict doctype fallback for non-XHTML browsers, WAI AAA, etc) and I find it’s actually easier to leave the XML declaration in the code always. IE5 (which is what IE6 essentially is) has a decent quirks mode. Really who wants to code to a “standard” that is anything remotely like IE5/6?

    … and I’ll be using XP until Linux gurus decide to remove the need to waste time in the console just to install programs such as Firefox and Opera. It seems all OS’s are doomed to committing GUI suicide these days.

    BTW… this page has an XML declaration. ;-)

  5. Eric

    I fought with this bug for a very long time. Instead of removing the XML declaration, I have found that sending a PHP header(“Content-type: text/html; charset=utf-8″) and removing the content-type meta tag also keeps Exploder 6 from going into quirksmode, at least for me with my particular setup. However, I still can’t send compressed CSS if I use this method, as Exploder 6 will intermittently and unpredictably fail to load the stylesheet. YMMV.