First off, to anyone new around here, I recommend you go read the original XHTML/HTML post and all the comments.
Having read several hundred lines more, I have come to the conclusion that converting XHTML 1.1 to HTML 4.01 Strict is an unnecessary complication. While sending the slightly modified XHTML 1.1 as HTML 4.01 is a complex and debatable topic that I'll try and stay out of in this post, we will, however, need to occasionally touch it.
To start off with, let's quote Steven Pemberton, the chair of the W3C HTML Working Group from the W3C mailing list in 2000:
David,
The HTML WG has discussed this issue: the intention was to allow old (HTML-only) browsers to accept XHTML 1.0 documents by following the guidelines, and serving them as text/html. Therefore, documents served as text/html should be treated as HTML and not as XHTML. There should be no sniffing of text/html documents to see if they are really XHTML.
Note that there are some semantic differences between HTML documents and XHTML documents: there are specific CSS rules that only apply to HTML (and not XHTML), and the DOM has different effects (for instance, the element names are returned in uppercase for HTML, and lower case for XHTML).
Best wishes,
Steven Pemberton
Chair, W3C HTML WG
This clearly lays out the fact that sending XHTML as text/html was what was intended for legacy support, however, the W3C note on XHTML Media Types make it plain and obvious that XHTML 1.1 should not be sent as text/html - this leaves us in a dilemma, we're meant to send XHTML as text/html for legacy support, but not send XHTML 1.1 as text/html.
So, taking our conclusion as we're meant to use a doctype switcher to switch between XHTML 1.1 served as application/xhtml+xml for browsers that allow it, as well as the validator, and serve everything else XHTML 1.0 Strict served as text/html, as long as we meet the HTML Compatibility Guidelines (appendix C of the XHTML 1.0 specification).
So, as ever, I'm going to post a PHP version, and ask anyone who can to port this to other serverside languages, and send it to me, so I can post it here giving them credit.
<?phpif ((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="utf-8"?><!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 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> <meta http-equiv="content-type" content="<?php echo $mime; ?>; charset=utf-8" />
Comments
Gary says…
August 21, 2005 02:30:15+01:00
Sound as a pound.! Though I must admit I wasn't convinced by the somewhat esoteric arguments against stripping trailing slashes for html 4.01 browsers and serving as html/text.
Still at least you've given us both options now.
Mike says…
September 7, 2005 23:19:30+01:00
Brillient piece of work!! Very good job after some trial & error worked like a charm!! Thanks for the script!