XHTML

Reasons for creating documents in XHTML

The W3C has developed XHTML to be the future markup language for the World Wide Web, on personal computers and other devices such as palmtop computers and speech browsers.

The key advantages of XHTML over HTML are:

Modularity
XHTML is extensible and so further modules can be added that are not covered in the basic XHTML Document Type Definition. An example of this is adding mathematical equations to XHTML documents inline. The MathML module can achieve this, with mathematical expressions enclosed in the MathML root element math. See some MathML examples for more information.
XML compliance
XML-compliant markup, such as well-formed XHTML, can be parsed by XML tools. Then powerful XML technologies are available such as true access to the DOM for scripting and the application of XSL for styling. Browsers can be much smaller and simpler programs because they do not have to guess the intention of the author - the markup will be well-formed and, if the author is professional, valid.
Separation of style from structure
Versions of XHTML from 1.1 onwards do not allow presentational elements such as font and marquee. XHTML1.0, XHTML1.1 and XHTML2.0 insist that CSS and XSL are used for presentational formatting. Note that this formatting is not limited to visual layout: it could mean other formatting such as the tone, volume and pitch of a spoken voice, or the order in which the document's contents are delivered.

This document is an example of XHTML1.1. The elements are in fact HTML tags so the document can be rendered in modern (version 5+) browsers. There is a growing movement within the internet development community to create valid (the markup conforms to a published standard), well-formed (tags are closed and ordered correctly) documents as these will be readable on the widest possible number of browsers and platforms without errors.

If a document is invalid or not well-formed, browsers may try to guess the author's intent, or just generate an error message. Either result is undesirable.

Authors should note that:

  1. XHTML1.1 does allow inline comments, scripts and styles, but they must be used with caution: if they use the <!-- ... --> comment notation within an element (such as to hide JavaScript from very old browsers), XML parsers may or may not parse the text inside the comments. Comments can be included as XML CDATA by using <![CDATA[ ... ]]>, and scripts and styles should be given by using <script type="text/javascript" src="URI"></script> and <link type="text/css" rel="stylesheet" href="URI" /> elements respectively.
  2. The name attribute is deprecated in favour of the id attribute. They both use the same namespace (memory) and so you can include both in most instances to make your document backwards-compatible.

Versions of XHTML

As with any language, XHTML is evolving to become more powerful and useful. The main versions of XHTML are:

XHTML1.0
The Transitional, Strict and Frameset versions of XHTML1.0 are simply reformulations of HTML4.01 in XML-compliant markup. The only differences between them are that Frameset allows the document to contain child frames, Transitional is for a document that has legacy markup such as the font and target elements, and Strict is for documents that rely on stylesheets for formatting. XHTML1.0 is broadly backwards-compatible with browsers that can render HTML4.01. Note that care should still be taken with inline comments within scripts and styles, as the document must conform to the rules of XML.
XHTML Basic
Newer versions (1.1+) of XHTML are modular. That is, extra elements can be added into the core html markup if and when a suitable Document Type Definition or XML Schema exists. The basic set of HTML elements and attributes are described by XHTML Basic, which is particularly aimed at "lightweight" user agents such as palm-top browsers and mobile devices.
XHTML1.1
Developed from XHTML1.0 Strict, XHTML1.1 takes modularity to fruition by allowing inline MathML, SMIL, SVG and similar XML-based languages. Most of the pages on tomoakley.net are written in XHTML1.1 for this reason. At the time of writing it is the latest version of XHTML recommended by the W3C.
XHTML2.0
Version 2.0 breaks from the legacies of HTML4.01 and is not intended to be backwards-compatible with basic HTML browsers. More details will be posted here as they become available. The author does not recommend the use of XHTML2.0 because it is likely to change and is not supported in any meaningful way by user agents.

Browsers

Internet Explorer version 5.5+ supports XML and XHTML1.1 well, but does not understand the application/xhtml+xml MIME type that the server should send before the XHTML document.
Amaya supports XHTML1.1 fully.
Firefox supports XHTML1.1 well.

Using XHTML with PHP

Using the popular hypertext preprocessor PHP with XHTML may cause problems because:

  1. PHP can be confused by the <? string at the start of the XML declaration.
  2. PHP sends documents as text/html rather than the correct content type of application/xhtml+xml.

Use the following steps to get round these problems:

  1. Add a few lines of PHP to the beginning of the XHTML file that send the correct header and avoid problems with the XML declaration, such as:
    <?php
    if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
    // user agent accepts XHTML explicitly
    header("Content-type: application/xhtml+xml");
    }
    else {
    // user agent may be a search engine, incompatible browser or validator -
    // send incorrect MIME type to deliver at least some text
    header("Content-type: text/html");
    }

    // send XML declaration even if short tags <? are enabled
    echo("<?xml version=\"1.0\" ?>\n");
    ?>
  2. Save your files to your server with the extension .php
  3. Set your server to process .php files with PHP. On Apache you can do this by inserting the line
    AddType application/x-httpd-php .php
    into your .htaccess file.

Definitions

Well-formed
A well-formed XML document conforms to the tight rules of XML (which are well documented elsewhere), such as ensuring all elements are closed, that all elements and attributes are in lower case and so on. Documents can be checked for well-formedness by the W3C Validator.
Valid
Documents are only considered valid if the conform to a Document Type Definition or an XML Schema (laid out in an XSD. An example of a document type declaration that specifies a DTD is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">



Message:

Privacy policy