I am quite new to XSLT and quite confused on how to use java inside XSLT. I have an XSL template that will transform one XML to another. My requirement is to access an external java class method in my XSLT. I have a java class called FileCopy and its method is copyFile(). I need a way to access the copyFile() method inside my XSLT.
1 Answer
You have to bind the extensions to the stylesheet. It should be via namespaces, class names, and the Java class path.
For example, define your class:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:filecopy="java:com.test.FileCopy"
exclude-result-prefixes="filecopy">
and then you can use it:
<xsl:value-of select="filecopy:copyFile($params)"/>
More information is here
9 Comments
Kevan
The class path comment is important of course. For testing what I do is, in the above example, put the java class in a sub folder test of a sub folder com of the folder that contains the style sheet so that it will be found by the xslt engine
nishMaria
I have my xsl file in say C:/transform. I placed my java file in C:/transform/com/test. Once i run the xsl i get the error -ERROR: 'Cannot find class 'java:com.test.FileCopy '.' FATAL ERROR: 'Could not compile stylesheet' Invalid factory configuration javax.xml.transform.TransformerConfigurationException: Could not compile stylesh eet
Andriy Budzinskyy
is "com.test" your package for FileCopy? I've just add it as example. If there is no package, you can try xmlns:filecopy="java:FileCopy".
nishMaria
I have a folder C:/transform and my xsl template and java class resides here. So now the structure will be C:/transform/test.xsl and C:/transform/FileCopy.java. when i run the xsl, i stil get the error i mentioned above "'Cannot find class 'java:com.test.FileCopy "
Andriy Budzinskyy
ok, can you open FileCopy.java and check package name at the top of file? If it does not exist, remove "com.test" in xstl file: xmlns:filecopy="java:FileCopy"
|