Some older browsers such as Netscape 3.0 and Internet Explorer 4.0 do have varying degrees of difficulty with XHTML constructs such as empty-element tags. For example, Netscape 3 places two horizontal lines when it sees <hr></hr> and none at all when it sees <hr/> . It is unwise to use the full panoply of XML syntax when serving XHTML to browsers. In particular, here are my suggestions.
None of this should significantly affect what you can or cannot do in a web page. There are alternatives for all of these that work in older browsers. For instance, instead of CDATA sections, you can simply escape all literal less-than signs and ampersands as < and & , respectively. However, if this proves too limiting for you, there is an alternative. You can author and validate your content in XHTML, then convert it to HTML before serving it. The simplest way to do this is with an XSLT stylesheet that reads XHTML and outputs plain- vanilla HTML. You can either preconvert all the documents before placing them on the web server or automatically convert them when they're requested , using one of the techniques discussed in Item 44. Example 46-1 demonstrates . Example 46-1 A Stylesheet That Converts XHTML to HTML<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml"> <xsl:output method="html"/> <xsl:template match="@*node()"> <xsl:copy> <xsl:apply-templates select="@*node()"/> </xsl:copy> </xsl:template> <xsl:template match="html:*"> <xsl:element name="{name(.)}"> <xsl:apply-templates select="@*node()"/> </xsl:element> </xsl:template> </xsl:stylesheet> This stylesheet copies all nodes that are not elements in the XHTML namespace to the output tree. Nodes that are elements in the XHTML namespace are also copied . However, they're changed to elements with the same local name that are not in any namespace. If you start with valid strict XHTML and then use this stylesheet, you're guaranteed HTML that's about as perfectly compatible with existing browsers as possible. In fact, I'd venture to say that if you find a browser that can't display what comes out of this process reasonably, there's about a 99% chance that the bug is in that browser, not your code. |