Invalid XHTML

XHTML is somewhat different from HTML in the way that most browsers treat the code. Where with HTML the browser will attempt to make sense of invalid code, this does not happen in most browsers when you have XHTML.

With HTML you can omit closing tags and the browser will add them for you where it thinks that they belong (which is not necessarily going to be the same place in all browsers). Misplaced closing tags will also have one inserted where it belongs with the one in the actiual code being ignored. Similarly when you leave out any of the optional opening tags that too will be inserted into the page by the browser at the spot where the browser thinks it belongs. Threese changes made by the browser will not show up when you view the source of the page but they will be reflected in the Document Object Model when you access the page from JavaScript.

Basically with HTML the browser will try to correct any errors that you make for you. While validating an HTML page will tell you what errors you have in the HTML so that you can correct them, it isn't going to make a huge difference if you don't fix many of them because the HTML will still be able to be processed, the CSS will probably still work and the JavaScript might still do what you expect. With HTML you don't need to fix the errors in order for the browser to be able to process the HTML but you might need to fix some of the errors in order for specific JavaScript code to work correctly.

Everything is completely different with XHTML. The XHTML standard does not require browsers to handle any coding errors in the XHTML. In most of the web browsers that actually support XHTML the XHTML page will not be displayed if there are any errors in the code at all. For example suppose we have the following XHTML web page that contains two errors:

header("Content-Type: application/xhtml+xml; charset=utf-8");
echo '<'.'?xml version="1.0" encoding="UTF-8"?','>';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
<head><title>Real XHTML</title>
<p>This page is real XHTML but it has some tags missing and so should not be able to be displayed.</html>

Now of course whether your XHTML is valid or not it will still be offered for download by browsers that cannot display XHTML at all (such as Internet Explorer 8 and earlier). Where the browser does support XHTML most will report an error when you have any errors in the XHTML instead of displaying the web page.

Because the close of the paragraph and body from an XHTML web page are missing from the above XHTML only Internet Explorer 9 will still display the page. If we try to display the page in Firefox 5 we get the following message instead of our web page:

XML Parsing Error: mismatched tag. Expected: </p>.
Line Number 6, Column 101:

The browser has reported the first mismatched tag that it found. Even if we fix that error by adding back the close of the paragraph we will still get another error message because the closing body tag is still missing.

To see how the various browsers handle XHTML errors simply view the invalid XHTML web page in each browser.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow