Keeping Extensions Portable


As soon as your stylesheet uses extension functions, or other permitted extensions such as extension instructions or extension attributes, keeping it portable across different XSLT processors becomes a challenge. Fortunately, the design of the XSLT language anticipated this problem, and offers some help.

There are a number of interrogative functions that you can use to find out about the environment that your stylesheet is running in. The most important are as follows :

  • The system-property () function, which allows you to determine the XSLT version supported and the name and version of the XSLT processor itself.

  • The function-available() function, which allows you to determine whether a particular extension function is available. This is particularly useful when you are using a third-party library such as EXSLT, where the same functions may be available under a number of different XSLT processors.

Use these functions to test whether particular vendor extensions are available before calling them. The best way to do this is using the new use-when ‰« attribute described in Chapter 3, which allows a section of the stylesheet (perhaps a whole template, perhaps a single <xsl:value-of > instruction) to be conditionally included or excluded from the stylesheet at compile time. For example, the following code sets a variable to the result of the random:random-sequence() function (defined in EXSLT) if it is available, or to the fractional seconds value from the current time if not.

  <xsl:variable name="random-number"   select="seconds-from-time(current-time()) mod 1.0e0"   use-when="not(function-available('random:random-sequence', 2))"   xmlns:random="http://exslt.org/random"/>   <xsl:variable name="random-number"   select="random:random-sequence(1, ())"   use-when="function-available('random:random-sequence', 2)"   xmlns:random="http://exslt.org/random"/>  

(In fact, the use-when ‰« attribute on the first <xsl:variable> declaration is not strictly needed, assuming these are local variables . If the use-when ‰« attribute on the second variable evaluates to true, this variable will shadow the first variable, and it does no harm for both variables to be present.)

Note that the rules for the use-when ‰« attribute require it to be a condition that can be evaluated at compile time. It's therefore not permitted in this expression to reference the values of variables or stylesheet parameters, or to access the contents of a source document.

You can use similar techniques to make a stylesheet portable between different XSLT versions. In this case there are additional facilities available, notably the [xsl:] version attribute, which can be attached to any element in the stylesheet. Version compatibility is fully discussed in Chapter 3, on page 123.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

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