Monday, September 13, 2010

What about the XML declaration?

As stated earlier, there is an exception to the DTD being the first thing on a web page. The
one thing that can precede it is an XML declaration (often referred to as the XML prolog).
This unassuming piece of markup looks like this (assuming you’re using Unicode
encoding):
<?xml version="1.0" encoding="utf-8"?>
The tag tells the browser which version of XML is being used and that the character encoding
is UTF-8.
Some web design applications add this tag by default when creating new XHTML documents,
and the W3C recommends using it to declare the character encoding used within
your document. However, I don’t, because versions of Internet Explorer before version 7
take one look at the XML declaration, recoil in horror, and then spit out your site in a way
rather different from how you intended (the playfully-referred-to quirks mode discussed
earlier).Of course, Internet Explorer 6’s share of the market is in decline, but it’s likely to take at
least a couple of years from Internet Explorer 7’s release for its predecessor to become
extinct. Therefore, because the XML declaration has the potential to cause havoc with a
fair chunk of your likely audience, it’s cause for concern. However, as mentioned earlier,
there’s an alternative, compliant, totally safe option that you can use instead:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Using the preceding meta tag works fine, it does the same job as one of the main roles of
the XML declaration (stating the page’s character encoding), and no browsers choke on it.
The net result is that everyone goes home happy, and we can finally start talking about the
next part of a web page.

No comments:

Post a Comment