REVISION – 2006/12/05: The code originally posted here would not work, so it has been duly corrected.
Table of Contents
- Why Doesn’t XML Work in IE?
- Why Doesn’t IE XML Work in Safari?
- How Do I Make XML Work in Both IE and Safari?
- My Code Doesn’t Work!
Why Doesn’t XML work in IE?
XML does work in Internet Explorer; in fact, Internet Explorer has a very capable XML parser. As with other browsers, to activate it, set the Content-type of your document to text/xml.
PHP:
<?php
header ("Content-type: text/xml");
?>
ASP/ASP.NET:
<%response.ContentType="text/xml"%>
You can also do this in your .htaccess file if you don’t use a server technology.
Be sure to place an XML declaration at the beginning of your file.
XML:
<?xml version="1.0"?>
If you’re using PHP, your server will likely think <? and ?> are PHP declarations, so you’ll have to print them out:
PHP:
<?php print "<?xml version=\"1.0\"?>\n"; ?>
Once this directive is set, most browsers should parse your code as XML, but in Internet Explorer you’ll likely see an XML tree instead of your source code. This can be fixed calling an XSLT style sheet using the following code:
XML:
<?xml-stylesheet href="/xslt/xhtml.xsl" type="text/xsl"?>
If you’re using PHP, print it out:
PHP:
<?php
print "<?xml-stylesheet href=\"/xslt/xhtml.xsl\"
type=\"text/xsl\"?>\n";
?>
Now create the new file at the location you’re pointing to (/xslt/xhtml.xsl) and fill it with the following:
XSLT (XML) :
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform"> <template match="/"> <copy-of select="."/> </template> </stylesheet>
Internet Explorer can parse XML, just not XHTML (that is, XHTML with the MIME type of application/xhtml+xml).
Why Doesn’t IE XML work in Safari?
The simple answer is that the only way to turn Internet Explorer’s XML parser on is to use invalid code. Why is the code invalid? Look at the style sheet declaration we just used:
XML:
<?xml-stylesheet href="/xslt/xhtml.xsl" type="text/xsl"?>
Notice that we’re using the MIME type text/xsl. Technically there is no such MIME type as text/xsl, since XSLT files are really just XML files. If you change text/xsl to text/xml, it will work in Safari, but Internet Explorer will not understand the XSLT stylesheet anymore and will go back to only returning the XML parse tree.
How Do I Make XML Work in Both IE and Safari?
I haven’t found a way to get IE and Safari to co-operate using traditional means (thank you, Apple/Microsoft feud). Fortunately, we can hack!
We won’t use JavaScript browser detection for two reasons:
- Client-side scripting makes it mandatory for your site’s visitors to have JavaScript enabled.
- We can’t. We’d have to declare a
<script>tag before our<html>tag, which isn’t valid XML or HTML, and so it’d cause errors in all browsers and it’s likely our page wouldn’t display at all.
If we use a server-side script, however, we can detect the browser our visitor is using and from that output the code we’ll use! I’m using PHP and StyleGala‘s hack to detect if the browser is IE. If so, I’ll print the <?xml-stylesheet?> declaration; otherwise, I won’t bother, since most browsers can parse XML without this XSLT style sheet.
PHP:
<?php
header ("Content-type: text/xml; charset=iso-8859-1");
print "<?xml version=\"1.0\"?>\n";
$d = detect();
$b = $d['browser'];
$v = $d['version'];
$o = $d['os'];
function detect()
{
$browser = array ("IE","OPERA","MOZILLA","NETSCAPE",
"FIREFOX","SAFARI");
$os = array ("WIN","MAC");
$info['browser'] = "OTHER";
$info['os'] = "OTHER";
foreach ($browser as $parent)
{
$s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),
$parent);
$f = $s + strlen($parent);
$version = substr($_SERVER['HTTP_USER_AGENT'], $f, 5);
$version = preg_replace('/[^0-9,.]/','',$version);
if ($s)
{
$info['browser'] = $parent;
$info['version'] = $version;
}
}
foreach ($os as $val)
{
if (eregi($val,strtoupper($_SERVER['HTTP_USER_AGENT'])))
$info['os'] = $val;
}
return $info;
}
if ($b == 'IE')
print "<?xml-stylesheet href=\"/xslt/xhtml.xsl\"
type=\"text/xsl\"?>\n";
?>
And there we have it! Our code works in both Internet Explorer and Safari!
My Code Doesn’t Work!
“I can’t use ! Safari says ‘Entity nbsp Not Defined’.”
Use   instead.
“My div won’t centre in Internet Explorer!”
You either have to use positioning to place the document halfway across the page and margins to correct it, or declare text-align: center to the parent element of your div. Examples and descriptions of both these methods can be found here.
