RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


XSLT Extension Functions in XDK 10g

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

SLT 1.0 provides a way for developers to call implementation-specific extension functions from within a stylesheet. In the Oracle XSLT processor for Java, you use XSLT extension functions to access Java class functions (methods) from an XSLT stylesheet, letting you use all Java's power to augment native XSLT transformations.

The Java methods you access from XSLT may be either static or non-static methods. This article explains how to configure XSLT extension functions, as implemented in XDK 10g, for use in an XSLT stylesheet, parsing and modifying an XML document using extension functions as an example.

Preliminary Setup

To follow along, you'll need the Oracle command-line XSLT processor utility oracle.xml.parser.v2.oraxsl, from the XML Developer's Kit (XDK) 10g Production for Java, which you can download here. Extract the xdk_nt_10_1_0_2_0_production.zip file to an installation directory of your choice. Add <XDK>/lib/xmlparserv2.jar to your CLASSPATH¸ where <XDK> is the directory where you extracted the .zip file.

Parsing with XSLT Extension Functions

For this example, you can use the catalog.xml XML document shown below (also available in the downloadable code). Copy the catalog.xml file to a c:/catalog directory on your hard drive:

<?xml version = '1.0'?>
<catalog title="Oracle Magazine" publisher="Oracle Publishing"> 
  <journal date="Jan-Feb 2005"> 
     <article section="PL/SQL">    
       <title>Refactoring for PL/SQL Developers</title>    
       <author>Steven Feuerstein  </author>   

Typically you'd use XSLT extension functions to:

  • Perform a conversion. For example you might need to convert Fahrenheit to Celsius, or perform math functions that are not included in XSLT's library functions.
  • Create XSLT variables from result values. It's sometimes convenient to get the results of a process into an XSLT variable. Using extension functions is a natural way to achieve that.

It's important to note that this example intentionally makes no modifications that you can't do in standard XSLT. Instead, it focuses on demonstrating all the features of XSLT extension functions, which are:

  1. Use of static methods.
  2. Use of non static methods.
  3. Use of custom classes.
  4. Use of variables with values obtained with extension functions.
  5. Using constructor extension functions.

You can use standard XSLT techniques to check the transformation.

After modifying the input document with the XSLT extension features, it'll look slightly different, as shown below. Specifically, the <journal> and <article> element attributes have changed, as have the <title> and <author> element values:

<?xml version = '1.0'?>
<catalog title="Oracle Magazine" publisher="Oracle Publishing">   
   <journal date="January-February 2004">    
   <article section="Inside OCP">               
     <title>Oracle Certified Master</title>             
     <author>Jim Dillani</author>     

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