elefant
elefant

Reputation: 73

Calling Java method with Variable Argument in XSLT

I have a Java method with variable argument which I need to call from XSLT (1.0). But it doesn't work, it keeps failing with the following error:

javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet

However, if I expand the variable argument, it works!

Example:

 public static String getValue(String key, String... args) {  // this is what I want, but doesn't work

 public static String getValue(String key, String args1) { // this works

 public static String getValue(String key, String args1, String args2) { // this works

In XSLT:

<xsl:value-of select="myjavaclass:getValue('MyKey','Arg1')"/>

<xsl:value-of select="myjavaclass:getValue('MyKey','Arg1','Arg2')"/>

So, does anyone know if I can use Java method with variable argument in XSLT? I hope I don't have to create different methods for each number of argument.

Thank you much!

Upvotes: 3

Views: 2506

Answers (5)

user4365812
user4365812

Reputation: 1

I got the same issue , but it works for me to use the following instead.Hope it does for you guys

public static String getValue(String key, String args) { 
String[] parameters = args.split("####");
// here you got you args array   

}

and concatenating all the arguments in the String args variable:

<xsl:value-of select="myjavaclass:getValue('MyKey',concat('Arg1','####','Arg2','####','Arg3'))"/>

Upvotes: 0

dcco
dcco

Reputation: 75

Old question, but in case anyone else stumbles over here and wants to pass in a String parameter, a hack you could use to get around not being able to pass in a String array directly is to have a helper function that builds the String[], ie first call a function that gives an empty array, maybe with a default given length; then do as many calls to a function that adds the parameters to that array as you need; then pass the array in as a parameter.

It would probably only be useful in a pathological case but.

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122364

As far as reflection is concerned a varargs method like

public static String getValue(String key, String... args)

is a method with two parameters, the first is a String and the second is a String[]. When you call a varargs method from Java code the compiler generates bytecode to bundle up the variable args into an array, but when you're calling by reflection, as will be the case here, you have to build the array yourself.

So the question becomes, does your XSLT processor provide any way to pass parameters of type String[] from XSLT to Java? In the case of Xalan (the default javax.xml.transform implementation if you don't have another one on your classpath) I believe the answer is no, except in the special situation where the String[] was returned by another extension function.

Upvotes: 3

Michael Kay
Michael Kay

Reputation: 163262

The mechanisms for calling Java methods from XSLT depend on which XSLT processor you are using, so it's not possible to answer this question without knowing.

In addition, the exception message that says compilation was unsuccessful isn't useful. We need to see the specific error messages that were passed to your ErrorListener (the default ErrorListener probably writes these to System.err or to some log file, but again that depends on what product you are using.)

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34367

Since your first argument is also of type String, I think its causing the issue.

Try

     public static String getValue(String... args) {
           String key = args[0];
           String arg1 = args[1];
           .....  
     }

Upvotes: 1

Related Questions