|
Home > Online Product Documentation > Table of Contents > Specifying Extension Functions in Stylesheets Specifying Extension Functions in StylesheetsYou can write XSLT extension functions in Java and invoke them in XPath expressions in stylesheets. This section provides instructions for implementing and invoking extension functions from your stylesheet. The syntax supported is a subset of that supported by LotusXSL/Apache Xalan. This section covers the following topics: Using an Extension Function in Stylus StudioThe process of using an extension function in Stylus Studio involves three main steps:
1. First, you need to write a Java class that can be used from within a stylesheet. In this example, the
SystemDate() method returns the system date and time as a string:
2. Second, compile your class and register it on the Stylus Studio host by copying the
.class file to a location defined in the host's
CLASSPATH environment variable.
3. Finally, specify information in the stylesheet so that Stylus Studo can use your class. You do this with a namespace reference in the
xsl:stylesheet tag. For example, define a namespace as
xmlns:Ext where
Ext is the prefix to use when calling the class methods. (
Ext is not a predefined keyword; it can be replaced by any other legal string.) The namespace reference then takes the class name as a value. In this example, the whole reference looks like the following:
The class is now available from within the stylesheet and can be used in a template such as the following:
The XSLT stylesheet might look like the following:
Basic Data TypesXPath and XSLT data types map to Java data types according to Table 30:
Declaring an XSLT Extension FunctionExtension functions must have one of the following signatures:
public Object FxnName()
public Object FxnName(Type1 var1, Type2 var2,...)
public static Object FxnName()
public static Object FxnName(Type1 var1, Type2 var2,...)
A class that contains an extension function might look like the following:
Working with XPath Data Types
The XPath types
The XPath processor determines the actual return type of a function at run time. For example, the XPath processor treats the return type of the function in the preceding section as an XPath
Declaring an Extension Function NamespaceIn conformance with the XSLT specification, extension functions are accessed through a unique namespace. The namespace declaration can be in any of the following locations:
The XPath processor treats the namespace URI as a fully qualified class name. If the class name is preceded by
You can separate package names with either a dot (
The XPath processor resolves namespace prefixes in names of extension functions relative to the namespace declarations in the stylesheet. Invoking Extension FunctionsYou use XSLT extension functions just like built-in XPath functions. For example:
Finding Classes and Finding Java
The XPath processor looks for extension classes by using the
The XPath processor tries to load the Sun Java Runtime Environment (JRE) 1.4.x or later. If the XPath processor cannot find a suitable JRE, invoking Java extension functions causes an error during stylesheet processing. In Stylus Studio, classes are reloaded each time you refresh the preview output, so changes in a class are reflected in subsequent preview output. Debugging Stylesheets That Contain Extension Functions
You can use Stylus Studio backmapping and debugging features on stylesheets tht invoke extension functions. You must process the stylesheet with one of the following processors: The Xalan-J and Saxon processors do not allow you to step into JavaScript extensions. You can step into Java extensions, however. |

Cart

