XHTML
XHTML introduction
XHTML purpose
XHTML/HTML diff
XHTML syntax
XHTML DTD
HTML to XHTML
XHTML validation
XHTML modules
HTML 5
XHTML history
XHTML summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

HTML to XHTML conversion

XHTML is the current standard and HTML is the previous. Despite this, there are currently many webpages on the web that need to be converted into XHTML.

This lesson focuses on:

Why convert to XHTML?

You have a website with 12 pages, each page has about 200 - 300 lines of code. Your pages looks fine in a browser, and you see no reason to convert the code to XHTML. But there is a reason, there are in fact several reasons.

Tag and syntax rules

To convert your pages from HTML to XHTML, there are some tag and syntax rules you need to make sure your code is following.

Tag rules

Tag rules
  • Tags in XHTML must be closed in the proper order
  • Tags in XHTML must be closed
  • Tags (as well as their attributes) in XHTML must be in lowercase
  • XHTML documents can only have one root tag

For more details on these tag rules visit our differences between HTML and XHTML page.

Tags must be closed in proper order

This is wrong:

<p>Visit <a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </p></a>

This is right:

<p>Visit <a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </a></p>

Tags must be closed

This is wrong:

<a href="http://www.yahoo.com" target="_blank"> Yahoo search engine <img src="/images/pic1.jpg" alt="Picture 1">

This is right:

<a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </a> <img src="/images/pic1.jpg" alt="Picture 1" />

Tags (and their attributes) must be lowercase

This is wrong:

<a HREF="http://www.yahoo.com" target="_blank"> Yahoo search engine </a> <IMG SRC="/images/pic1.jpg" alt="Picture 1" />

This is right:

<a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </a> <img src="/images/pic1.jpg" alt="Picture 1" />

XHTML documents can only have one root tag

The root tag of XHTML documents is <html>. All the other tags of an XHTML document must be nested within it. They can have sub-elements within them, all of which must be nested properly within their parent elements.

Basic structure of an XHTML document:

<html> <head> <title>document title</title> </head> <body> document content </body> </html>

Syntax rules

Syntax rules
  • Attribute names must be in lower-case
  • Attribute values must be quoted
  • Attributes cannot be minimized
  • The name attribute is replaced by the id attribute
  • The xmlns attribute should be applied to the <html> tag
  • Some elements are mandatory

For more details on XHTML syntax rules visit our XHTML syntax page.

Attribute names must be in lower-case

This is wrong:

<a HREF="http://www.lycos">

This is right:

<a href="http://www.lycos">

Attribute values must be quoted

This is wrong:

<a href=http://www.lycos>

This is right:

<a href="http://www.lycos">

Attributes cannot be minimized

This is wrong:

<textarea rows="20" cols="20" readonly></textarea>

This is right:

<textarea rows="20" cols="20" readonly="readonly"></textarea>

The name attribute is replaced by the id attribute

This is wrong:

<img src="/images/pic1.jpg" name="pic1" />

This is right:

<img src="/images/pic1.jpg" name="pic1" id="pic1" />

NOTE: The name and id attributes should be used together for backwards compatibility.

The xmlns attribute should be applied to the <html> tag

The attribute xmlns stands for XML namespace. This attribute is needed because an XHTML document is based on XML. The URL given as the value for this attribute is where to find the namespace.

This is wrong:

<html>

This is right:

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

Some elements are mandatory

XHTML documents must contain a doctype declaration. Mandatory XHTML elements include the html, head, body, and title elements. The title element must be located inside the head element.

A basic XHTML document with the mandatory elements (including the doctype declaration):

<!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"> <head> <title>XHTML is cool</title> </head> <body> XHTML is used to display content on webpages </body> </html>

NOTE: The doctype declaration is NOT an XHTML element, and not part of the XHTML document. The doctype declaration is used to declare which document type the XHTML document is.

The doctype declaration as well as the different document types you can have in XHTML are covered in the XHTML document types lesson.

Confirm page validity

Once you convert your pages from HTML to XHTML, everything is good and clean, right? Well maybe, but not necessarily. You have to actually confirm that your pages are valid.

You can do this with the validator available at validator.w3.org.

If a page validates right away, great! If it does not, it's ok. Keep working at it. If a page does not validate, the validator will tell you where the errors are so that you can fix them. Once you fix the error(s), re-test as much as you need until you get a valid page.

You can also download and use the program HTML Tidy to fix errors in your HTML code.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments