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

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

Here is an example, which defines two alternative entry points, one for an XSLT 1.0 processor and one for an XSLT 2.0 processor. This assumes that the

element specifies
version = “2.0”
. This means that an XSLT 1.0 processor will be running in forward-compatible mode (explained in the next section) and will therefore ignore attributes such as
use-when
that it does not understand. An XSLT 1.0 processor will use the first template rule as the entry point, because it has higher priority. An XSLT 2.0 processor, however, will behave as if the first template rule is not present, and will use the second one, which differs in that it invokes schema validation of the result document.

              use-when=“system-property(‘xsl:version’)=‘1.0’”>

  



  

    

  


The expression contained in the
use-when
attribute can be any XPath expression, but it is constrained to have a very restricted evaluation context. This means there is no context item, there are no variables available, and no access to external documents. In practice, this means that the only things the expression can usefully do is to examine the results of functions such as
system-property()
,
element-available()
, and
function-available()
, to see what environment the stylesheet is running in. These three functions are fully described in Chapter 13.

One important reason for the introduction of the
use-when
attribute was to allow stylesheets that work both on schema-aware and non-schema-aware XSLT processors to be written. For example, you can use the
system-property()
function in a
use-when
attribute on the

declaration so that a schema is imported only when using a schema-aware processor. For details of how schemas are imported into a stylesheet, see Chapter 4.

Version Compatibility

Version compatibility is about how to achieve resilience to differences between versions of the XSLT standard.

There are currently two versions of the XSLT Recommendation, versions 1.0 and 2.0 (the many intermediate working drafts don't count). So compatibility between versions has now become an issue. However, the language designers had the foresight to anticipate that it would become an issue, and made provision even in version 1.0 to allow stylesheets to be written in a portable way.

The stylesheet is required to carry a version number (typically
version = “1.0”
or
version = “2.0”
) as an attribute of the

element. Specifying
version = “1.0”
declares that the stylesheet is designed primarily for use with XSLT 1.0 processors, while specifying
version = “2.0”
indicates that it is designed for XSLT 2.0.

The term
backward compatibility
refers to the ability of version
N
of a language to accept programs or data that worked under version
N
– 1, while
forward compatibility
refers to the ability of programs that worked under version
N
to move forward to version
N
+ 1. The two concepts are therefore opposite sides of the same coin. However, the XSLT language specification distinguishes carefully between them. As far as an XSLT 2.0 processor is concerned, a stylesheet that specifies
version = “1.0”
is operating in backward-compatible mode, while a stylesheet that specifies
version = “3.0”
is operating in forward-compatible mode.

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

Other books

Athena's Son by Jeryl Schoenbeck
Inevitable by Heiner, Tamara Hart
Born to Lose by James G. Hollock
Obsession in Death by J. D. Robb
Conjurer by Cordelia Frances Biddle
Whispers of Love by Rosie Harris
Howards End by E. M. Forster
Up From the Blue by Susan Henderson
The Gargoyle at the Gates by Philippa Dowding