There has always been a lot of discussion about which one to use, and people have attacked this from different angles, with arguments including:
- Internet Explorer enters "Quirks Mode" when an XHTML document has the XML prologue, yet has a 70% marketshare
- If there's a slight error in a XHTML page, being correctly served as
application/xhtml+xml browsers which support it fail to render it - XHTML is the future, so why stay with HTML?
So, basically, if we were able to hand out equally valid XHTML 1.1 and HTML 4.01 Strict depending on whether the UA supports application/xhtml+xml, all our problems have gone? Wrong. Browsers that don't support CSS will still fail, so make sure you use structured, semantic mark-up, then, even without styles, it will 99% of the time, look alright, and be readable, and understandable :)
But, how are we meant to sniff wheather the UA supports application/xhtml+xml, and through that XHTML? Javascript? No, it can be disabled. So what? PHP, or another server side language.
If your server supports PHP, here's the code:
<?php
// Charset
$charset = 'utf-8';
function fix_code($buffer) {
$str = (str_replace(" />", ">", $buffer));
return (str_replace("xml:lang", "lang", $str));
}
if ((stristr($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml')) || (stristr($_SERVER["HTTP_USER_AGENT"], 'W3C_Validator')) || (stristr($_SERVER["HTTP_USER_AGENT"], 'WDG_Validator'))) {
$mime = 'application/xhtml+xml';
} else {
$mime = 'text/html';
}
header ("Content-type: $mime");
If ($mime == "application/xhtml+xml") {
echo '<?xml version="1.0" encoding="' . $charset . '"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
} else {
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">';
ob_start("fix_code");
}
?>
<html <?php if ($mime == "application/xhtml+xml") { echo'xmlns="http://www.w3.org/1999/xhtml"'; } ?> xml:lang="en">
<head>
<meta http-equiv="content-type" content="<?php echo $mime; ?>; charset=<?php echo $charset; ?>" />
I cleaned out this version, putting the settings at the top, making easier for someone who doesn't know PHP to use, but compromising a couple of milliseconds :P
If anyone can convert this to ASP they will be thanked, get the code put up in the post and be creditied for it...
The above represented my views at the time, however, things change, so please read XHTML/HTML Followup.
[Updated 10th January 2005 - Adding xml:lang to lang support]
[Updated 10th January 2005 - Bug Fix]
[Updated 2nd March 2005 - Mistakes pointed out by Mithoric]
[Updated 21st August 2005 - New Views]