Older Browsers

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.

  • Include at least one space before the /> in an empty-element tag.

  • Do not use CDATA sections.

  • Do not use processing instructions such as xml-stylesheet , especially in the document prolog.

  • Do not use character references such as &#x111; or &#968; .

  • Do not use entity references that were not available in HTML 1.0 such as &rdquo; and &tau; .

  • Do not include an internal DTD subset.

  • Do not use an XML declaration. Encode the document in UTF-8 and use an HTML http-equiv meta tag to indicate that.

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 &lt; and &amp; , 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.

Effective XML. 50 Specific Ways to Improve Your XML
Effective XML: 50 Specific Ways to Improve Your XML
ISBN: 0321150406
EAN: 2147483647
Year: 2002
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net