Friday, August 7, 2009

Valid XHTML document


An XHTML document is technically an XML document. XML documents can include an XML declaration as the first line in the document, something like this:
<?xml version="1.0" encoding="UTF-8"?>

There are only a few major rules to consider, but you have to follow them if you want to create a valid XHTML document. Here they are in brief:

* In an XHTML document every tag must be closed.
* Empty elements without content, must be correctly closed with a closing slash.
For example, a break tag is formatted <br />.
* Tags must be nested correctly,
it works like a LIFO queue, the last tag you open must be closed first.
* All XHTML tags should be in lowercase.
* All attribute values are enclosed in quotation marks.
* A valid XHTML document needs a valid XHTML DOCTYPE declaration.

The DOCTYPE declaration works for several purposes:
* DOCTYPE helps your page to be validated as XHTML.
* DOCTYPE allows the browser knows the version of your page markup language.
* DOCTYPE references the specific DTD for your page markup language.
* DOCTYPE enables your page to be displayed properly in standard web browsers
(IE, Netscape Navigator, Mozilla, Firefox, Opera, Chrome, and ...).

For an XHTML 1.0 document, you can choose one of three different DOCTYPES:
- strict
- transitional
- frames

- strict DOCTYPE declaration:
If for presenting and styling your document you are using CSS.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

- transitional DOCTYPE declaration:
If your document includes any presentational or styling markup code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

- frames DOCTYPE declaration:
If your document is in frames.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

The easiest to use is the transitional DOCTYPE declaration, but using CSS to present document sometimes is the best way.

After XHTML DOCTYPE declaration, it must have an additional line of markup:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

In XHTML, the above line works instead of the opening <html> in HTML document. It adds information about the XHTML namespace. (A namespace is basically a vocabulary of elements and attributes)
Share/Bookmark

No comments: