Serving XHTML as XML in IE
Hey all.
I've recently come across a PHP script which allows me to serve a "text/html" media type to IE and an "application/xhtml+xml" media type to Opera and Mozilla while using essentially the same markup. (A good thing because I like the strict way in which XML parses markup.) So, I make a few minor modifications to the script (adding an XML Stylesheet here, switching a DTD there), see that both the HTML and XHTML versions of my code validates and go on my marry little way.
I later find out that although IE lacks support for the "application/xhtml+xml" media type--and thus no way to serve XHTML as XHTML--there is a way to serve XHTML as XML using "application/xml." (Which means I get XML parsing for IE. Joy.) Of course, I jump on that. I make further modifications to the original script: I get rid of the now useless $buffer variable, I make both DTDs XHTML 1.0 Strict, replace "text/html" with "application/xml", etc. I fire it up in IE and I immediately notice two things.
Firstly, I can't view the source file. Instead, I get a dialog box which says "The XML source file is unavailable for viewing." Though it was admittedly initially kind of cool knowing that IE indeed interpreted the document as XML, but eventually it became annoying because I'm using all kinds of php includes and I need an easy way to see what the post-processed XHTML (or perhaps XML) code looks like.
Secondly, I've noticed that something about the encoding just is not right. I'm not particularly knowledgeable in this aspect of web design (I usually just set it to iso-8859-1 and call it a day) so I'm sorry that I can't be more specific. I did note that IE initially set the character encoding to UTF format even though I explicitly stated that I wanted ISO. That lasted until I cleared the cache. Now, when I right click in IE it claims to be using the correct (?) Western European encoding, but with no other possible options. The bullet points on my unordered list suggest to me that it's still set to UTF, or at least not ISO.
So, what I'm asking is why IE will not let me use view the source and if there is a resolution to my encoding dilemma. I'm also asking for any skilled PHP coders to give my modifications a once over. I'm new to this whole PHP thing and I might've done something wrong which caused both these problems. Any help would be much appreciated.
Here is my site. (Yes, I am aware that makes nonsense out of my CSS. I'm working on it. .) Here is the modified PHP script (the original is refrenced in the first link):
<?php // The original script is courtesy of Key Stone Websites <http://keystonewebsites.com/articles/mime_type.php>.
$charset = "iso-8859-1";
$mime = "application/xml";
$declaration = "<?xml version=\"1.0\" encoding=\"$charset\" standalone=\"no\"?>\n";
$css = "<?xml-stylesheet title=\"Smiley\" href=\"style/smiley.css\" type=\"text/css\" media=\"screen\"?>\n";
$xsl = "<?xml-stylesheet type=\"text/xsl\" href=\"copy.xsl\"?>\n";
$dtd = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
$namespace = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-us\" dir=\"ltr\">\n";
if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
if(preg_match("/application\/xhtml\+xml;q=([01]|0\.\d{1,3}|1\.0)/i",$_SERVER["HTTP_ACCEPT"],$matches)) {
$xhtml_q = $matches[1];
if(preg_match("/application\/xml;q=([01]|0\.\d{1,3}|1\.0)/i",$_SERVER["HTTP_ACCEPT"],$matches)) {
$xml_q = $matches[1];
if((float)$xhtml_q >= (float)$xml_q) {
$mime = "application/xhtml+xml";
}
}
} else {
$mime = "application/xhtml+xml";
}
}
if($mime == "application/xhtml+xml") {
$prolog_type = "$declaration $css $dtd $namespace";
} else {
ob_start("fix_code");
$prolog_type = "$declaration $xsl $dtd $namespace";
}
header("Content-Type: $mime; charset=$charset");
header("Vary: Accept");
print $prolog_type;
?>
Finally, I did slightly modify the "copy.xsl" file (the original is refrenced in my second link) in an effort to get the correct encoding:
<?xml version="1.0" encoding="iso-8859-1"?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
<output method="xml" version="1.0" encoding="iso-8859-1"/>
<template match="/">
<copy-of select="."/>
</template>
</stylesheet>