start page | rating of books | rating of authors | reviews | copyrights

Book HomeWeb Design in a NutshellSearch this book

31.3. XHTML Document Declarations

Every XHTML file must begin with declarations that tell the browser which versions of XML and XHTML are used in the document. The browser uses this information to display the document correctly. In Chapter 9, "Structural HTML Tags" we saw how a DOCTYPE declaration can be added to the beginning of an HTML document. For XHTML, this element is required.

The following is a minimal XHTML document:

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

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Title is required</title>
</head>

<body>
..content of document...
</body>

</html>

Let's look at this sample one element at a time. The document begins with an XML declaration (beginning with <? and ending with ?>) that tells the XML processor that the document uses XML Version 1.0.[16] The encoding attribute specifies the 8-bit Unicode character set (the most common). Be sure that the encoding value matches the character set used in your document (see Chapter 7, "Internationalization" for more information on character sets).

[16]Some older browsers (such as Navigator 3) that don't understand XML display the XML declaration text as part of the displayed content. Therefore, if you're sending XHTML documents to these older browsers, you may need to omit this part.

The next element is the DOCTYPE declaration (the same as we've seen in XML and even regular HTML documents) that references the Transitional DTD, both by a public identifier and via a URL. If your document follows the strict XHTML version, use this declaration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

And if it is a framed document, use the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

Finally, there are a few more directions added within the <html> tag for the document. The xmlns attribute specifies the primary namespace for the document. A namespace is a unique collection of tags and attributes that can be referenced by the browser. Namespaces are discussed in Chapter 30, "Introduction to XML". More than one namespace may be referenced within a document, for instance, if the document contains other XML applications such as Scalable Vector Graphics (SVG) or MathML.

The lang attribute is used to declare that the document language is English, for both the XML and XHTML namespaces.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.