Calling Java Instance Methods in XSLT

I use Saxon (I could use Xalan, if necessary) XSLT processor to perform some conversions. I want to pass an instance of the object below into an XSLT template as a parameter.

public class Test { private String value; public Test(String v) { value = v; } //getters, setters etc. } 

So, I create this object, i.e.

 Test test = new Test("test"); transformer.setParameter("test", test); 

In the XSLT file, I declare param as:

 <xsl:param name="test" required="yes" as="jt:com.whatever.package.Test" xmlns:jt="http://saxon.sf.net/java-type"/> 

Now my question is: how can I call any instance method (i.e. getValue ()) for this object in XSLT? Is it possible? I know that I can call static methods of different Java classes, but this is not quite what I am looking for.

Also, is it possible to populate Java objects in XSLT, i.e. set setter methods for an instance of an object, and then use this object with the new values ​​in the Java code after the conversion is complete?

+4
source share
4 answers

You should be able to call instance methods of the "external object" passed as a parameter in the form that you describe. If $ object is such an object, and com.package.MyClass is its class, and you want to call the getColor method for this object, then

(a) you need to declare a namespace such as xmlns: Myclass = "java: com.package.MyClass"

(b) you call the method as MyClass: getColor ($ object)

This Java calling mechanism is referred to in Saxon as “reflective extension functions”. It is not supported in Saxon Home Edition. You will need either the Saxon Professional Edition or the old open source Saxon-B product. Another mechanism in Saxon-HE is called "integrated extension functions", but it takes a bit more coding on the Java side to declare argument and result types.

You need to know that with the help of reflective extension functions, Saxon does its best to map Java types to XPath types and does not always do the mapping as you would like, especially when using the types collection.

Try to avoid using methods with side effects, such as customization methods. There is no absolutely reliable way to ensure in Saxon that such calls are made in any particular order, and sometimes the Saxon optimizer will find a way to organize a request that avoids making a call at all. If you must make such calls, process them as if the call returned a result (for example, an empty sequence) and used the call so that when the result was returned, the result would appear in your style sheet output.

+4
source

Here is explained in detail :

http://www.saxonica.com/html/documentation/extensibility/functions/

A brief example :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="java:java.util.Date" exclude-result-prefixes="date"> <xsl:template match="/"> <html> <xsl:if test="function-available('date:to-string') and function-available('date:new')"> <p><xsl:value-of select="date:to-string(date:new())"/></p> </xsl:if> </html> </xsl:template> </xsl:stylesheet> 

when applied to any XML document (not used) creates the desired, correct result:

 <html> <p>Sat Oct 06 11:41:30 PDT 2012</p> </html> 
+3
source

I have done this with Xalan a long time ago. First, you need to add the following to your xslt (I don’t think you need all the other attributes that you set)

 <xsl:param name="test" /> 

and then to call the method you can do

 <xsl:value-of select="test:getValue()"/> 
+1
source

Too late I publish, but for some it may be useful.

You can easily achieve this with the xalan processor:

Step 1: enable Xalan dependency or suitable jar

 <!-- https://mvnrepository.com/artifact/xalan/xalan --> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.1</version> </dependency> 

Step 2. Select this processor when integrating with java

 TransformerFactory factory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null); 

Step 3. In xsl, enter the information in the stylesheet

 xmlns:uuid="xalan://PackageName.className" 

Note that this class must have a Static method that you will use in xslt

Step 4: use it in xslt

 <xsl:value-of select="uuid:methodName(string(xpath))" /> 

It can be a string or number..depending on a method argument

Source: http://www.cafeconleche.org/books/xmljava/chapters/ch17s03.html

0
source

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


All Articles