XSLT Extension Functions in XDK 10g : Page 2

While XSLT alone is powerful, extending it with implementation-specific Java functions gives you all the power you could want.

by Deepak Vohra

Jun 12, 2006

Page 2 of 4

Declaring Extension Function Namespaces

To configure XSLT extension functions in an XSLT you must declare the namespace of the extension functions (Java class methods) in the XSLT Stylesheet. This example uses Java class methods from the classes:

oracle.xml.parser.v2.DOMParser

oracle.xml.parser.v2.XMLDocument

oracle.xml.parser.v2.XMLPrintDriver

org.w3c.dom.Element

org.w3c.dom.Node

org.w3c.dom.NodeList

Add the namespace declarations for these Java classes to the xsl:stylesheet element of the XSLT. The syntax for declaring a Java extension function namespace is:

Using the syntax example above, <classprefix> is a name you choose that you'll later use in the stylesheet to invoke methods on the matching Java class, and <classname> is the Java class on which you want to call the methods. For example, here are two of the namespace declarations for the classes listed above:

You can see working examples of these namespace declarations in the sample XSLT file shown in Listing 1.

XSLT extension functions do not have any provision for casting Java objects; however, because many select calls to retrieve XML DOM objects return Node objectseven though you actually want to work with an Element objectyou often need to make typecasts, such as from Node to Element. To convert one class to another you need a custom Java class. The example XSLT (see Listing 1) uses the custom Java class NodeToElement.java to cast org.w3c.dom.Node types to org.w3c.dom.Element types. Here's the code for the NodeToElement class:

Instantiating Java Classes

...where <classobject> is the name of the XSLT variable you will use to refer to the class object in the XSLT, and <classprefix> is the namespace prefix for the Java class that you declared in the xsl:stylesheet element of the XSLT (see the preceding section).

The param1, param2, param3 items in the preceding syntax represent any constructor parameters needed to create a class instance.

You can see an example in the root template of the example XSLT stylesheet, which creates a DOMParser object, using the code:

<xsl:variable name="parser" select="parser:new()"/>

In a similar fashion, you create a java.io.File instance and a java.io.FileReader instance using this code: