Click here to Skip to main content
13,355,275 members (59,943 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 1 Feb 2004

8 simple rules to transition from HTML to XHTML

, 1 Feb 2004
Rate this:
Please Sign up or sign in to vote.
8 simple rules to transition from HTML to XHTML



This article assumes that you have at least a fair idea of HTML


By now, you must have a fair idea of what XHTML is and why it should be adopted. If not, read Paul Watson’s article on XHTML, from an HTML starting point.

Most articles on the web are currently authored in HTML, and not very good HTML at that. So, how do you go about transforming poorly written HTML into XHTML?

This was the question I was faced with while doing a redesign of a Federal government website. The website was huge (40,000 pages) and I was working on the top 30 page templates. This is what I learnt from this challenging and exciting project:

One, add the DOCTYPE

The DOCTYPE is the SGML declaration of the version of the current document. The DOCTYPE should be the first line of your XHTML document unless you use an xml declaration such as the one below to specify character encoding, in which case the DOCTYPE should immediately follow the xml declaration. The default character encoding is UTF-8 or UTF-16.

<?xml version="1.0" encoding="UTF-8"?>

There are three types of Document Type Definitions or DTDs: Strict, Transitional and Frameset.

The Strict DTD should be referenced when the markup in the document follows all HTML rules to the letter.

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


The Transitional DTD is the best to use when moving from HTML to XHTML.

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


The Frameset DTD is referenced when the page layout uses frames.

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"


Two, specify the namespace

For the document to be a valid (more on that later) XHTML document, it is necessary to specify the namespace – browsers use the default in the absence of a specified namespace.

Change your opening html (<html>) tag to look like this:

<html xmlns="" xml:lang="en" lang="en">

There is no change to the closing html (</html>) tag.

Three, Change all tags to lowercase

You may be used to writing your HTML tags in uppercase to differentiate the tags from your data, but XHTML requires that all tags be written in lowercase. XHTML is case-sensitive, so

<HTML> is not considered the same as

<html> which is the correct way of writing it.

Four, Close every tag you open

We know that in HTML, certain tags have to be closed and others like the <img> tag, don’t. In XHTML, all tags have a closing tag. Tags like <br> (empty elements) should now be written as <br /> with a space between the tag and the forward slash and no space between the forward slash and the angle bracket. The space between the tag and the forward slash allows backward compatibility with older browsers. Tags with data such as the <p> tag can be closed by using the </p> tag. The img tag is considered as an empty tag and is closed as below:

<img src=”myimage.jpg” alt=”The author with the Pope” />

Five, Make sure all elements are properly nested.

That means the innermost tag should be closed first, followed by the one opened before it, and so on.

<p> <b> This is wrong </p> </b>

It should be correctly written as:

<p> <b> This is right </b> </p>

Six, Quote all attribute values

In XHTML, it is mandatory that all attribute values are listed within quotes. This means the following is wrong.

<img src=myimage.jpg />

The correct way to do it is as below. Also, for every image on the screen (except for spacers and non-informative, decorative images), it is a good practice to have meaningful, interpretive text listed in the alt tag.

<img src="myimage.jpg" alt="The author with the Pope" />

Seven, add the id attribute to elements that have the name attribute.

The name attribute is being deprecated and the id attribute is taking its place. However, for backward compatibility, it is best to leave in the name attribute and to add the id attribute. The id attribute should be added wherever name attributes are used, such as the a, applet, form, frame, iframe, img, and map tags. The syntax of the id attribute is similar to that of the name attribute and is written as id=”value”.

Eight, Validate

Now that you are done, how do you know that you got it all right? That’s where validation comes in – remember I said I would talk about it later? Now that you have an XHTML document, it is time to check that it really follows all the rules listed above. One way to do this is to go over the entire document and look at each and every line of code. Two things – a) This is boring and, b) It doesn’t guarantee anything.

So, the next best thing to do is to use a validator, such as the one provided by the W3C at You may upload a file from your local machine or provide the url to be validated.

Another easy way to do it is to save the file as an xml file – filename.xml and to open it in your browser. If it displays correctly, it is valid XHTML. If not, you will be able to tell immediately where the problem lies. There you go – its so simple, you have no excuse to put it off anymore.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Subha Subramanian
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionmissing something? a solution? Pin
Derek Read6-Feb-04 8:38
memberDerek Read6-Feb-04 8:38 
AnswerRe: missing something? a solution? Pin
Subha Subramanian9-Feb-04 12:10
memberSubha Subramanian9-Feb-04 12:10 
Hi Derek,
Thanks for your feedback. I do think that most people have their own level of comfort with HTML and may choose a variety of tools to do the conversion from HTML to XHTML. I did however find that Homesite worked very well for me, especially since it has a feature that let me do an extended search and replace - it would search all the files and files in the subfolders for the location chosen. This spared me the laborious task of going through a huge number of files to make the same changes and also reduced the chances of accidentally introducing errors while making changes to a file. Homesite also has several options that you may set to create a valid XHTML document such as formatting code to standards with HTML Tidy.

Ms. Subha Subramanian
AnswerRe: missing something? a solution? Pin
Maciek Niedzielski3-Mar-04 9:29
memberMaciek Niedzielski3-Mar-04 9:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 2 Feb 2004
Article Copyright 2004 by Subha Subramanian
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid