Browse DevX
Sign up for e-mail newsletters from DevX


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.


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:

import org.w3c.dom.*;

public class NodeToElement{
    public static Element nodeToElement(Node node){
       Element element=null;
        if(node instanceof Element)
       return element;


Instantiating Java Classes

In general, the syntax to create a class object in XSLT is...

<xsl:variable name="<classobject>" 
   select="<classprefix>:new('param1', 'param2', 'param3')"/>

...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:

<xsl:variable name="xmlDocument" 
<xsl:variable name="file-reader" 

Parsing the XML

Now you can parse the example XML document with the DOMParser class's parse( java.io.Reader) method:

<xsl:value-of select="parser:parse($parser, $file-reader)"/>

The parse method is a non-static method of the DOMParser class. The syntax for a Java class non-static method in an XSLT is:

   <methodparam1>, <methodparam2>)"

By now this should begin to look familiar, but just to be sure:

  • <classprefix> is the namespace prefix for the Java class specified in the xsl:stylesheet element.
  • <classmethod>is the Java class method you want to call.
  • <classobject> is the XSLT variable representing the class instance on which you want to call the non-static method.
  • <methodparam1>, <methodparm2> are the method parameters.

Thanks for your registration, follow us on our social networks to keep up-to-date