There are a lot of people who are confused as to exactly what the difference is between HTML and XHTML. This web page for example is written in HTML. If you view the source of the page there are not too many clues as to which this page actually is (unless you are using a browser such as Internet Explorer which doesn't support XHTML at all in which case the mere fact that you are viewing the page at all is proof that it is HTML).

Not sure why I say you can't tell easily from the source? Well if you view the source of this page you will see that it has an XHTML transitional doctype. Doesn't that make it XHTML? Well no, it just means that it can be XHTML. The fact that the page validates as XHTML is a further indication that it might be XHTML but is still not proof. Valid XHTML can still be served as HTML provided that it complies with the rules in Appendix C of the XHTML standard. Of course if you view the JavaScript attached to the page and find references to document.write(), innerHTML or document.createElement() that are a working part of the page then you have your proof that the page is HTML since none of those JavaScript statements work with XHTML. With XHTML you will see commands like document.createElementNS() which passes the namespace that XHTML requires as the second parameter to the call.

There are two things that are required to make a page real XHTML and one of those is a header setting and so the simplest way I can demonstrate it to you is by showing you tyhe source of a PHP page that creates real XHTML.

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. If your browser displays this as a web page then it supports XHTML. If instead the file was offered for download then your browser doesn't support XHTML.</p>

The two pars of that which are in bold are what makes this particular XHTML page into real XHTML. As you will see if you click on the link to that page IE offers the real XHTML file for download since it is a format that can't be displayed in that browser. Most other browsers will display the page as easily as they display HTML. If you are using a browser that supports it go ahead and view the source of the page. What you will notice is that the key element that makes it XHTML rather than HTML (the Content-Type) is not visible in the source of the page (that's why I used PHP for this example so as to add it in the page source (you can of course configure your web server to serve all files with a particular extension as XHTML by associating them with the Content-Type.

The other thing which the page must have in order to be XHTML is the eXtended Markup Language NameSpace (xmlns) defining the namespace that the HTML tags within the XHTML belong to. The page may of course have additional namespaces defined where tags from other libraries are used to provide additional functionality within the XHTML page. Of course you can't use any of those other libraries with HTML because HTML doesn't support namespaces.

So why "pretend" that you are writing XHTML when you are really writing slightly invalid HTML? (since the closing slashes in self closing tags are invalid in HTML we precede them with a space so that they don't invalidate the preceding attribute and make the document unworkable as HTML). Well in my case it is a preference for the stricter rules that the XHTML validator detects since HTML has many tags defined as optional (so that the browser must know how to handle when someone leaves them out) which really ought to be there if you are writing a page that you expect to be maintainable in the future. By using the XHTML validator I can ensure that those tags which are mandatory in my standard but optional as far as the people writing the browsers are concerned are properly tested for. Also at the time I started writing the pages the expectation was that the next version of all the browsers that didn't already support XHTML would add that support and that by the time everyone was using a version 5 browser that XHTML would be the norm and HTML would be dead. I know that my pages are HTML despite what the doctype says, the XHTML validator serves the purpose that I require of it and my pages work properly as HTML.

Note that all my XHTML tutorials show you how to write code that is both valid as XHTML, can be used as is as slightly invalid HTML, or can be easily adapted by changing the doctype and removing a few slashes into valid HTML.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow