demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

The xml prolog and doctype

Specifying the correct doctype of the document is very important. Without it, the client (the web browser, iPhone, etc) will have no idea which version of HTML you are using to write the document, and will default to guessing how the HTML should be rendered – in many browsers, this is known as “Quirks” mode. By specifying the xml prolog and doctype, we tell the client “render this document according to the standards of this specification”. That information is added at the very start of the page, as follows:

For XHTML, this is:

  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”>

The first part of the specification – the xml prolog – shows the close relationship between XHTML and XML. A strictly coded XHTML document can be parsed as XML, if we ever wanted to do so. This first line will tell the browser what version of XML the XHTML is transferable to (1.0) and the character set used on the page (utf-8, a version of Unicode). This line acts to “future proof” our work, and means that it will be readable for many, many years to come.

The next line tells the browser which version of XHTML we are using (1.0 Strict), what language it is written in (English), and where to find the defining document for the specification.

Note that neither the doctype nor the XML prolog are, strictly speaking, part of the XHTML language, and they largely ignore the rules that we are about to lay out so carefully. You may wish to think of them as a negotiation or an exchange of information between the web page and the browser, much as you might, upon meeting a stranger in Switzerland, attempt words in several different languages before both parties settle on speaking English. In our case, our web page is saying “Hi. I can give you the information that follows as XML or XHTML, whichever you prefer. Oh, and I'm going to speak in English, formating my characters this way (UTF-8), just so you know.”

The doctype is also required to validate our code: to check that we have written our XHTML in the right way. Without a doctype, the validator has no idea what version of (X)HTML you are using, or what rules to check your code against.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.