Call Java method with variable argument in XSLT

I have a Java method with a variable argument that I need to call from XSLT (1.0). But it does not work, it continues to fail with the following error:

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

However, if I extend 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 the Java method with a variable argument in XSLT? I hope I do not need to create different methods for each number of arguments.

Thank you very much!

+4
source share
5 answers

For reflection, the varargs type method is used

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

- a method with two parameters, the first is String , and the second is String[] . When you call the varargs method from Java code, the compiler generates bytecode to bind the args variables to the array, but when you call reflection, as with this, you must build the array yourself.

So the question is, does your XSLT processor allow you to somehow pass parameters like String[] from XSLT to Java? In the case of Xalan (the default implementation is javax.xml.transform , unless you have another in your classpath), I think the answer is no, except in a special situation where String[] was returned by another extension function.

+2
source

Since your first argument is also of type String , I think it causes a problem.

Try

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

The mechanisms for invoking Java methods from XSLT depend on which XSLT processor you are using, so it is impossible to answer this question without knowing.

Also, the exception message that says compilation was unsuccessful is not helpful. We need to see the specific error messages that were sent to your ErrorListener (by default, ErrorListener may write them to System.err or some kind of log file, but again it depends on which product you are using.)

+1
source

An old question, but in case someone else stumbles here and wants to pass a String parameter, the hack you could use to get around the ability to pass an array of strings is to have a helper function that creates String [], etc. e. first call a function that gives an empty array, possibly with a given default length; then make as many calls to a function that adds parameters to this array as necessary; then pass the array as a parameter.

This would probably be useful only in a pathological case, but.

0
source

I have the same problem, but for me it works instead. Hope this is for you guys.

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

and concatenation of all arguments in the String args variable:

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

Source: https://habr.com/ru/post/1445415/


All Articles