Recipe 14.1. Saxon Extension Functions
XSLT 1.0 (Saxon Version 6.5.4)
Saxon lets you
access
extension functions written in
Java by using the interface defined in the XSLT 1.1 draft standard.
Java is the only extension language currently supported by Saxon, so
extension function bindings are defined along the lines of the
following example:
<xsl:stylesheet
version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:Math="java:java.lang.Math"
exclude-result-prefixes="Math">
<xsl:script implements-prefix="Math"
xmlns:Math="java:java.lang.Math"
language="java"
src="java:java.lang.Math"/>
Here the naming convention used for
the
namespace is not strictly required. However, if followed, it makes
the xsl:script element optional. Hence, if you
need to access an extension only once, you can write something like:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="PI" select="4 * Math:atan(1.0)"
xmlns:Math="java:java.lang.Math"/>
<!-- ... -->
</xsl:stylesheet>
Here the namespace encodes the
binding
to the Java implementation rather than the
xsl:script. Note that these binding techniques are
independent; if you use the xsl:script element,
then the namespace's content does not matter. On the
other hand, if you omit the xsl:script, the
namespace has the sole responsibility of binding the Java
implementation.
XSLT 2.0 (Saxon Version 8.x)
The mechanism for invoking Java-based extension functions is largely
the same as Saxon 6.5.4. However, the saxon:script
element is deprecated and may be withdrawn in a future release
according to the product's web site. The preferred
method is to declare a top-level namespace of the form
java: followed by the fully-qualified class name.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="java:java.lang.Math"
exclude-result-prefixes="math">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<pi><xsl:value-of select="4 * math:atan(1.0)"/></pi>
</xsl:template>
</xsl:stylesheet>
|