2

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
  • Is there some other way to do file copy during XSL transformation rather than making a call to an external java class? Does XSLT support file copy? Commented Mar 9, 2012 at 12:49

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

9 Comments

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
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
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".
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 "
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"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.