Neal Grosskopf Dot Com A NG Designs Site

Neal Grosskopf Dot Com Return To Neal Grosskopf's Home Page a NG Designs Site

Toolbar

  • Skip To
    • Menu
    • Content
  • Validate
    • HTML
    • CSS
    • WAI
  • Font Size
    • Small
    • Medium
    • Large
    • Extra Large
  • Account
    • Create Account
    • Login

Interact

Navigation

  • Home
  • Interact
    • Blog
    • Message Board
    • Open Links
    • Open Videos
    • Photo Gallery
    • Polls
    • Reviews
    • Geek Speak
  • Designs
    • View All Designs
    • Professional
    • Corporate
    • Templates
    • Personal
    • Club
    • NG Designs
    • Web Guides
  • Files
    • Artwork
    • Wallpapers
    • My File Downloads
  • Extras
    • Bookmarklets
    • The Magic 8-Ball
    • Career Builder RSS Feed Generator
    • Monster.com RSS Feed Generator

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

  • Actions
    • View All
    • View Article Statistics
    • View User Statistics
    • Search
    • Subscribe to RSS Feed

Recent Articles:

  • Tracking Outgoing Links in Google Analytics Using Event Tracking and jQuery
  • Tracking Internal Website Campaigns With Google Analytics and PHP
  • Everything You Need to Know About jQuery's .data() Method
  • Google vs. China: How It Will Affect Web Workers
  • Don't Like The New Facebook Home Page? Fix It With 5 Lines of CSS
View All

Subscribe To Feed
loading Subscribers

I'm On Twitter loading

Jan 24

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

  • OT: How do you serve up XHTML?
  • XHTML 1.1 Conformance Definition
  • MIME Types and Content Negotiation
  • The <?xml> prolog, strict mode, and XHTML in IE
  • XHTML media type test - results: Microsoft Internet Explorer 7.0 Beta2
  • XHTML media type test - results
  • Cannot have a DOCTYPE declaration outside of a prolog. Error processing resource
  • Does Microsoft Internet Explorer accept the media type application/xhtml+xml?
  • XHTML: How to succeed
  • XHTML Deprecated Elements and Attributes

Tags: internet explorer, xhtml, xml, xsl ( Add Tag )

Bookmark This Page:
  • Digg loading
  • Delicious loading
  • Re-Tweet loading
  • StumbleUpon loading
  • DZone loading
  • Snipplr
  • Script & Style

Replies:

  • Matt Wilcox
    Matt Wilcox
    #1

    Wed. March 12, 4:43:43 AM

    Hi Neal. Good article, but I thought that IE does not support XHTML sent as XML? The browser used to prompt to download the file rather than serve it as a HTML page. That is why hardly anyone uses XHTML1.1. The point of Content Negotiation was to serve it as application/xml+xhtml to browsers that understand it properly, but as text/html to one's that don't. I've been using CN on my site for years, and IE always gets the text/html MIME.

    Firefox certainly works as expected though. Is your solution different somehow, and if so, is it still valid to send XHTML as application/xml rather than application/xml+xhtml ?

    http://www.autisticcuckoo.net/archive.php?id=2004/11/03/content-negotiation

  • Neal Grosskopf
    Neal Grosskopf
    #2

    Wed. March 12, 9:03:31 PM

    With the code I have above (the xsl I stole from the W3C's website - see my sources) you can actually serve xhtml as application/xml IE. The benefit of this is that your code has to conform to the strict xml validator. Click to view my test page for it which I use the example above.

  • Matt Wilcox
    Matt Wilcox
    #3

    Fri. March 14, 6:05:49 AM

    Ah, excellent!

    I had not previously realised that application/xml was an acceptable MIME type, I had thought it had to be application/xhtml+xml

    Nice, thanks for the code :)

  • Nore' Morse
    Nore' Morse
    #4

    Wed. July 28, 5:54:10 AM

    You test page throws error. Tried locally and got the same error.

    The XML page cannot be displayed
    Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


    --------------------------------------------------------------------------------

    The server did not understand the request, or the request was invalid. Error processing resource 'http://www.w3.org/TR/xhtm...

  • Neal Grosskopf
    Neal Grosskopf
    #5

    Wed. July 28, 7:28:24 AM

    Should work now.


Reply:

B U I Link Font Color Edit Preview ?
Characters: 0 Textbox Size: [+] [-]
   

  • New Comment
    #6

    Sat. July 31, 1:16:22 PM

Font Size:

  • A
  • A
  • A
  • A

Search:

Places:

  • Blog
  • Message Board
  • Chat Room
  • Geek Speak
  • Open Videos
  • Reviews
  • Tag Cloud
 

Site Links

Site

  • About
  • Contact Me
  • Site Map
  • Site Search

Interact

  • Blog
  • Geek Speak
  • Message Board
  • Open Links
  • Open Videos
  • Photo Gallery
  • Polls
  • Reviews

Random Stuff

  • Hosting by FatCow
  • © Copyright 2005 - 2010
  • Modified: Friday, July 16, 2010
  • Load Time: 0.04 Seconds

Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes