XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (165 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.69Mb size Format: txt, pdf, ePub

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

element. This means that if you want an XSLT 1.0 processor to invoke fallback behavior on a stylesheet that uses XSLT 2.0 features, you must either specify
version=“2.0”
on the

element, or specify
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

element.

Fallback processing means that if the unknown instruction has an

child element, the

instruction is evaluated; otherwise, an error is reported. If there is more than one

instruction, they are all evaluated.


is concerned only with fallback behavior for instructions within sequence constructors. Top-level declarations that the implementation doesn't recognize are simply ignored, as are unrecognized elements in another context (for example, an unrecognized child of an

or

instruction).

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

or

.

Usage

The

mechanism allows a stylesheet to be written that behaves sensibly with XSLT processors that handle different versions of XSLT. This is motivated by the experience of Web developers with HTML, and especially by the difficulty of writing Web pages that work correctly on different browsers. The design aimed to cater for a world in which some browsers would include an XSLT 2.0 processor and others would provide an XSLT 1.0 processor; in this situation, it would become necessary to write stylesheets that would work with either.

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

mechanism allows you to do this by defining within any proprietary extension element what the XSLT processor should do if it doesn't understand it. This might be, for example:

  • Do nothing, if the behavior is inessential, such as keeping statistics.
  • Invoke an alternative implementation that achieves the same effect.
  • Output fallback text to the user explaining that a particular facility cannot be offered and suggesting how they should upgrade.

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

: creating stylesheets that are forward compatible across XSLT versions, and creating stylesheets that use vendor extensions while maintaining portability. The third example is not actually an example of

at all: it shows another method of achieving fallback behavior when

won't do the job.

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

, so in practice the only way we could get this to work with xt was to use different overlay stylesheet modules for different processors. You'll find this solution in the subdirectory
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

; it's an example of where the same effect needs to be achieved by different means.

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

. The following code, which is perfectly legal in XSLT 2.0, will be flagged as an error by a conforming XSLT 1.0 processor. The message will be something like “cannot convert result tree fragment to node-set”:

Other books

The Glass Canoe by David Ireland
Mortal Obligation by Nichole Chase
Shredded by Tracy Wolff
The Celebrity by Laura Z. Hobson
Beauty by Sheri S. Tepper
Area 51: The Legend by Doherty, Robert