Page 1 of 1

PROBLEMS USING JAVA INTO XSLT

Posted: Wed Oct 13, 2010 6:23 pm
by ciscox83
Hi everyone,

I'm trying to call java function from my xslt. I use of course the Oxygen Editor.

I'm looking for a step by step guide that drive me in the entire process. Do you know something like that?

Or someone could help me in someway?

Thanks,
Chris

Re: PROBLEMS USING JAVA INTO XSLT

Posted: Thu Oct 14, 2010 10:53 am
by adrian
Here's a quick guide to get you started.

If you already have the stylesheet and Java code and just want to process the stylesheet in Oxygen, skip to step 3.

1. A simple XSLT example that calls a static java function, my.utils.io.LibFiles.moveFile(String src, String dst):

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="libfiles" version="1.0" xmlns:libfiles="my.utils.io.LibFiles">
<xsl:template match="/">
<xsl:variable name="srcFile">/path/to/my/file</xsl:variable>
<xsl:variable name="dstDir">/path/to/my/destination/directory</xsl:variable>

<result>
<xsl:value-of select="libfiles:moveFile($srcFile, $dstDir)"/>
</result>
</xsl:template>
</xsl:stylesheet>
2. A sample java class(my.utils.io.LibFiles) with the static function(It doesn't really move the file, just says it does):

Code: Select all

package my.utils.io;
public class LibFiles {
public static String moveFile(String a, String b) {
String toReturn = "Hello World. Moved '" + a + "' to '" + b + "'";
System.out.println(toReturn);
return toReturn;
}
}
Compile the java class or add it to a project in a proper Java IDE: Eclipse, NetBeans or IntelliJ IDEA

3. In Oxygen open/create the XSLT file(the example at 1.) and configure a transformation scenario(Document -> Transformation -> Configure Transformation Scenario, New).
a. In the XML URL field choose the XML to be processed by the stylesheet(can be a dummy XML for the example above).
b. Press the Extensions button and add to the list the folder where the compiled java classes are located(usually a 'bin' or a 'classes' folder from the Java project folder) or add jar files if you have any.

4. Run the transformation scenario(Document -> Transformation -> Apply Transformation Scenario).

Regards,
Adrian