Display Tags For 'xml'

How To Serve XHTML to Internet Explorer 6 And 7 as XML Using Content Negotiation

Neal Grosskopf
1/24/2008 9:02:24 PM

It seems that XHTML is all the craze now days. Unfortunately very few developers and designers are using it correctly and taking full advantage of it. So why is this?

XHTML can be served as text or XML. It seems that 90% of sites using XHTML are serving it as XML but are failing to serve it correctly to Internet Explorer as XML. Never fear, there is a solution.

So why bother serving it as XML to Internet Explorer? If you do it will mean that your html code will appear as XML and will have to adhere to XML standards. This means that you can be sure the code you are using is extremely compliant. It will also mean that if your page is not valid, it will not display without error. It's an annoying but great way to keep your code clean

Follow these steps:

  1. Serve your page as application/xml to Internet Explorer and as application/xhtml+xml to every other browser:

    If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 Then Response.ContentType = "application/xhtml+xml" Else Response.ContentType = "application/xml" End If
  2. Use a special .xsl file at the top of your page above your doctype:

    <?xml-stylesheet type="text/xsl" href="xhtml.xsl"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    Contents of xhtml.xsl file:

    <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform"> <template match="/"> <copy-of select="."/> </template> </stylesheet>
  3. Add html element to page:

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

Putting it all together:

<% If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 Then Response.ContentType = "application/xhtml+xml" Else Response.ContentType = "application/xml" End If Response.Charset = "utf-8" %> <?xml-stylesheet type="text/xsl" href="xhtml.xsl"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>XHTML</title> </head> <body> </body> </html>

Sources

Replies:8
Views:272471

<< Back