As an alternative to binding Java methods to functions in a stylesheet using the API, Java external functions can be declared directly within a stylesheet. The only additional configuration required is for bound Java classes to exist on the classpath during stylesheet execution.
<xltxe:java-extension
prefix = string
class = string />
prefix:methodName(Params*)
Invoking Static Methods
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xltxe="http://www.ibm.com/xmlns/prod/xltxe-j"
xmlns:calc="http://com.example/myApp/calculator"
xmlns:sf="http://com.example/myApp/standardFormat">
<xltxe:java-extension prefix="calc" class="org.company.Calculator"/>
<xltxe:java-extension prefix="sf" class="org.standards.Formatter"/>
<xsl:template match="/">
<xsl:value-of select="sf:format(calc:sqrt(64), 'ISO-42.7')"/>
</xsl:template>
</xsl:stylesheet>
// Create the factory
XFactory factory = XFactory.newInstance();
// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(xsltSource);
// Create the xml input
String xml = "<doc/>";
// Create a result object to store the transformation result
Result result = new StreamResult(System.out);
// Execute the XSLT executable
executable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())), result);
The example stylesheet provided assumes that the org.company.Calculator class contains a static method sqrt() that takes one parameter and the org.standards.Formatter class contains a static method format() that takes two parameters. At prepare time, the classes are not required on the classpath; but they are required during execution of the stylesheet.
package org.company;
public class Calculator {
public static int sqrt(int val) {
return (int)Math.sqrt(val);
}
}
package org.standards;
public class Formatter {
public static String format(int val, String pattern) {
return "Formatting " + val + " using pattern " + pattern;
}
}
Invoking Instance Methods
prefix:new(Params*)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xltxe="http://www.ibm.com/xmlns/prod/xltxe-j"
xmlns:car="http://com.example/myApp/car">
<xltxe:java-extension prefix="car" class="org.automobile.Car"/>
<xsl:variable name="var" select="car:new(3)"/>
<xsl:template match="/">
<xsl:value-of select="car:getDoors($var)"/>
</xsl:template>
</xsl:stylesheet>
// Create the factory
XFactory factory = XFactory.newInstance();
// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(xsltSource);
// Create the xml input
String xml = "<doc/>";
// Create a result object to store the transformation result
Result result = new StreamResult(System.out);
// Execute the XSLT executable
executable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())), result);
The example stylesheet assumes that the org.automobile.Car class contains a constructor that takes an argument of type int. In addition, the org.automobile.Car class also contains an instance method getDoors() that takes no arguments. The syntax for invoking instance methods from stylesheet-declared external functions require that the created instance object be passed in as the first argument.
package org.automobile;
public class Car {
private int doors;
public Car (int doors) {
this.doors = doors;
}
public int getDoors() {
return doors;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xltxe="http://www.ibm.com/xmlns/prod/xltxe-j"
xmlns:sedan="http://com.example/myApp/sedan"
xmlns:car="http://com.example/myApp/car">
<xltxe:java-extension prefix="sedan" class="org.automobile.Sedan"/>
<xltxe:java-extension prefix="car" class="org.automobile.Car"/>
<xsl:variable name="var" select="sedan:new(5)"/>
<xsl:template match="/">
<xsl:value-of select="car:getDoors($var)"/>
</xsl:template>
</xsl:stylesheet>
package org.automobile;
public class Sedan extends Car {
public Sedan (int doors) {
super(doors);
}
}