Jump to content

XHTML Content Negotiation and Conversion Script


Guest biolizard89

Recommended Posts

Guest biolizard89

Ever get pissed because you want to use XHTML to build a site, but Internet Explorer doesn’t support XHTML’s MIME Type? You could send it as HTML’s MIME Type, but then you don’t get any of the XHTML features, and many pages will break, e.g. things involving empty elements. Well, now you can get the best of both worlds! I’ve written a PHP script that you can add to any XHTML page, which will check the accept-type header, and automatically choose the correct MIME Type to send it as. But, even better, if it sends the code as HTML, it will automatically convert the whole document to HTML! This isn’t one of those crappy remove-slashes-and-hope-for-the-best hacks that you’ve seen floating around the Net. This uses 14 preg_replaces, and one str_replace, to solve the most common problems in converting XHTML to HTML. These are: * xml PI. * xml-stylesheet PI (alternate=”yes”). * xml-stylesheet PI (alternate=”no” or not specified). * Automatically replaces DOCTYPE. * xmlns attribute on <html> tag. * CDATA inside style or script. * CDATA outside of style or script. * Empty elements using trailing slash, e.g. <br/>. * Non-empty elements using trailing slash, e.g. <p/>. * Empty elements not using trailing slash, e.g. <br> </br>. * Non-minimized boolean attributes, e.g. checked=”checked”. * The xml:lang attribute. * The ' entity reference.Why is this necessary?XHTML has been the preferred general-purpose markup language from the W3C (succeeding HTML) since 2000. It has various features that make authoring in XHTML preferable to authoring in HTML for many applications. However, some browsers (most notably Microsoft Internet Explorer) do not support XHTML. As a result, many authors write XHTML code, but send it using the HTML MIME type. This is incorrect (see Sending XHTML as text/html Considered Harmful for details on why this is a bad idea). A much better solution would be to author your content in XHTML, but send it as either XHTML or HTML (using the appropriate MIME type) depending on whether the browser supports XHTML. Some authors always send XHTML source code, but with a different MIME type depending on the browser’s support for XHTML. This is also a bad idea, because it requires that the XHTML source code be HTML-compliant as well — which is usually not the case. For example, if your XHTML source code includes an empty element that closes itself using the <br/> shorthand, when you send it as an HTML MIME type, a compliant browser would show >s all over the place. Because of this problem, it becomes necessary to have the server convert the XHTML source code to HTML when sending a web page as an HTML MIME type. Hence this script.The script is in beta release 1.2 right now. I am asking for feedback, especially bug reports (I learned RegExps just for this script, so there are likely to be some small bugs in them). Feel free to use it on a production site (I am at the moment), as long as you are willing to make sure that it's not screwing up your site.For more information, and to download the latest version, visit its page on my Web site: http://staff.littlemoonproductions.com/bio...tiation-script/-Biolizard89PS: I PMed Jonas to make sure that this doesn't violate the advertising rule. Thanks for letting me post this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...