在XSLT中调用Java实例方法 [英] Call Java instance methods in XSLT

查看:228
本文介绍了在XSLT中调用Java实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Saxon(我可以在必要时使用Xalan)XSLT处理器进行一些转换。
我想将下面对象的实例作为参数传递给XSLT模板。

  public class Test {
private String值;

public Test(String v){
value = v;
}
// getters,setters等。
}

所以我创建了这个对象,即。

 测试测试=新测试(测试); 
transformer.setParameter(test,test);

在XSLT文件中,我将参数声明为:

 < xsl:param name =testrequired =yesas =jt:com.whatever.package.Testxmlns:jt =http:// saxon .sf.net / JAVA型/> 

现在我的问题是如何在此对象上调用任何实例方法(即getValue())在XSLT中?它甚至可能吗?我知道我可以调用不同Java类的静态方法,但这并不是我想要的。



此外,是否可以在XSLT中填充Java对象,即。调用setter方法作为对象的实例,然后在转换完成后将此对象与Java代码中的新值一起使用?

解决方案

您应该能够以您描述的方式调用作为参数传入的外部对象的实例方法。如果$ object是这样的对象,并且com.package.MyClass是它的类,并且你想在这个对象上调用方法getColor,那么



(a)你需要声明一个名称空间,例如xmlns:MyClass =java:com.package.MyClass



(b)你将方法调用为MyClass:getColor($ object) )



这种调用Java的机制在Saxon中被称为反身扩展函数。 Saxon Home Edition不支持它。您将需要Saxon专业版或旧的开源Saxon-B产品。 Saxon-HE中还有另一种称为集成扩展函数的机制,但它需要在Java端进行更多编码来声明参数和结果的类型。



您需要注意,使用自反扩展函数,Saxon会对如何将Java类型映射到XPath类型做出最佳猜测,并且它并不总是以您希望的方式进行映射,尤其是在使用集合类型时。 / p>

尽量避免使用带副作用的方法,例如setter方法。在Saxon中没有绝对可靠的方法确保这样的调用以任何特定的顺序执行,有时Saxon优化器会找到一种组织查询的方法,以避免拨打电话。如果你必须进行这样的调用,请将它们视为调用返回结果(例如空序列),并以这样的方式使用调用:如果它确实返回结果,则结果将显示在样式表输出中。 / p>

I am using Saxon (I could use Xalan if necessary) XSLT processor to do some transformation. I want to pass the instance of below object to the XSLT template as parameter.

public class Test {
   private String value;

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

So I create this object, ie.

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

In XSLT file I declare the 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 (ie. getValue() ) on this object within XSLT? Is it even possible? I know i can call static methods of different Java classes but thats not exactly what Im looking for.

Also, is it possible to populate Java objects within XSLT, ie. call setter methods for instance of an object and then use this object with new values in Java code after the transformation is completed?

解决方案

You should be able to call instance methods of an "external object" passed in as a parameter in the way you describe. If $object is such an object, and com.package.MyClass is its class, and you want to call the method getColor on 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 mechanism for calling out to Java is referred to in Saxon as "reflexive extension functions". It's not supported in the Saxon Home Edition. You will need either the Saxon Professional Edition, or the old open-source Saxon-B product. There's another mechanism in Saxon-HE called "integrated extension functions", but it requires a bit more coding on the Java side to declare the types of the arguments and result.

You need to be aware that with reflexive extension functions, Saxon is making best guesses as to how to map Java types to XPath types, and it doesn't always do the mapping in the way you would want, especially when using collection types.

Try to avoid using methods with side-effects, such as setter methods. There's no absolutely reliable way of ensuring in Saxon that such calls are executed in any particular order, and sometimes the Saxon optimizer will find a way of organizing the query that avoids making the call at all. If you must make such calls, treat them as if the call were returning a result (such as an empty sequence) and use the call in such a way that if it did return a result, the result would appear in your stylesheet output.

这篇关于在XSLT中调用Java实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆