Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Note that while XSLT 2.0 allows the
version
attribute to appear on any element in the XSLT namespace, XSLT 1.0 allowed it only on the
version=“2.0”
on the
xsl:version=“2.0”
on a containing literal result element. In practice, it is a good idea to put the templates and other declarations that depend on XSLT 2.0 in a separate stylesheet module, and label that module with
version=“2.0”
on the
Fallback processing means that if the unknown instruction has an
Note that both the
[xsl:]version
attribute and the
[xsl:]extension-element-prefixes
attribute apply only within the stylesheet module in which they occur: they do not apply to stylesheet modules incorporated using
Usage
The
Similarly, it is very likely that each vendor of an XSLT processor (or each browser vendor) will add some bells and whistles of their own—indeed, this has already happened with XSLT 1.0. For server-side stylesheet processing you might be prepared to use such proprietary extensions and thus lock yourself into the products of one vendor, but more likely, you want to keep your stylesheets portable. The
An alternative way of defining fallback behavior when facilities are not available is to use the
element-available()
function, perhaps within an
[xsl:]use-when
attribute, and thus to avoid compiling or executing the relevant parts of a stylesheet. This function is described in Chapter 13, page 764. The two mechanisms have overlapping functionality, so use whichever you find most convenient.
Examples
The two examples that follow illustrate the principal use cases for
Example 1: XSLT Forward Compatibility
The following example shows a stylesheet (
copy-to-output.xsl
) written to exploit a hypothetical new XSLT feature in version 6.1 of the standard that inserts a document identified by URI straight into the result tree. The stylesheet is written so that if this feature is not available, the same effect is achieved using existing facilities.
Example 2: Vendor Portability
Writing a stylesheet that uses vendor extensions but is still portable is not particularly easy, but the mechanisms are there to achieve it, especially in the case where several vendors provide similar extensions but in slightly different ways.
For example, several XSLT 1.0 processors (certainly xt, Saxon 6, and Xalan) provide a feature to generate multiple output files from a single stylesheet. With XSLT 2.0 this popular facility has made it into the XSLT standard, but before that happened, each product had to invent its own syntax. If you want to write a stylesheet that uses the XSLT 2.0 facility when it is available, but with fallback implementations for these three products, you should in principle be able to do it like this (
multidoc.xsl
):
xmlns:saxon6=“http://icl.com/saxon”
xmlns:xt=“http://www.jclark.com/xt”
xmlns:xalan=“http://xml.apache.org/xalan/redirect”
xsl:extension-element-prefixes=“saxon6 xt xalan”>
Preface
This would work if all three 1.0 processors implemented the full XSLT 1.0 specification. Unfortunately, xt doesn't support forward-compatible processing and doesn't recognize
ch06/fallback/xt
.
Hopefully, this little nightmare will disappear once XSLT 2.0 is widely implemented. However, by then the vendors, no doubt, will have thought of other good ideas to include as nonstandard extensions.
Example 3: Temporary Trees
This example doesn't actually use
One of the most important new features introduced in XSLT 2.0 is the ability to process a tree-valued variable as a document in its own right, using facilities such as XPath path expressions and