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:
-
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
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>
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