PROBLEMS USING JAVA INTO XSLT

Here should go questions about transforming XML with XSLT and FOP.
ciscox83
Posts: 1
Joined: Wed Oct 13, 2010 6:15 pm

PROBLEMS USING JAVA INTO XSLT

Post 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
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: PROBLEMS USING JAVA INTO XSLT

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply