Specifying Stylesheets for Different Browsers - Page 9
November 4, 2002
We've already seen that you need to specify a different XSL namespace in
your stylesheet depending on whether you're targeting XSLT 1.0 in the recent
browsers (IE 6 and Netscape 6) or XSL-WD in IE 5.0 or IE 5.5. This might seem
a major obstacle to supporting multiple browsers, since you need to change
the source XML document to reference a different stylesheet depending on the
browser.
In fact, the situation isn't too bad. It's possible to make sure each browser
gets an appropriate stylesheet using the alternate attribute.
As with CSS, it is possible to provide both one main, and several alternative
stylesheets with which to render a page. The main stylesheet is specified
in the <?xml-stylesheet ... ?> reference with alternate="no", and each of the alternative
stylesheets is specified with alternate="yes".
Fortunately, there is varying support for this in the different browsers,
which we can exploit to provide them with different stylesheets.
Rather than the <?xml-stylesheet ... ?>
reference we used in Example 1 above, we can instead use:
<?xml-stylesheet type="text/xsl" href="stylesheet_ie5.xsl"
alternate="yes" ?>
<?xml-stylesheet type="text/xsl" href="stylesheet_ie6.xsl" alternate="no"
?>
<?xml-stylesheet type="text/xsl" href="stylesheet_ns6.xsl" alternate="yes"
?>
This gives us different stylesheets for the different browsers because:
·
IE 5 and IE 5.5 don't understand the alternate
attribute, and will act on the first stylesheet found.
·
IE 6 does understand the alternate attribute, and will use the stylesheet
that has alternate="no".
·
Netscape 6 doesn't understand the alternate
attribute, and will act on the last stylesheet found.
The former two items can be trusted to work in future. The latter is a bug
in Netscape that may be fixed in future, but for the moment we can exploit
it to provide a different stylesheet for Netscape 6 and 7 to the one we provide
for the various versions of IE.
Typically, rather than the example above, it's more useful to use:
<?xml-stylesheet type="text/xsl" href="stylesheet_ie5.xsl"
alternate="yes" ?>
<?xml-stylesheet type="text/xsl" href="stylesheet_v6.xsl" alternate="no"
?>
because it's rare that you'll want to use a different
stylesheet for the two version 6 browsers.
In this chapter, we'll continue to just reference one stylesheet in our XML
for simplicity. When working through these examples, use the correct <xsl:stylesheet
... > tag in your XSLT stylesheets for whichever browser you use.
For browsers that support XSLT 1.0, this is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or, for XSL-WD:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
This does mean that in some cases, you'll have a lot of code shared between
the XSL-WD and the XSLT 1.0 stylesheets. The best way of avoiding this is
with server-side code to include the common templates in both files – using
SSI, ASP, JSP, or whatever. This does still retain the benefits of client-sdie
XSLT, but makes maintenance easier.
Transformation Without Change - Page 8
Practical XML for the Web
Example 2: Creating a Table of Contents with XSLT - Page 10
|