![](/img/trans.png)
[英]ERROR: 'The first argument to the non-static Java function 'evaluate' is not a valid object reference.' when using TrasformFactory
[英]XSLT - calling Java method - gives: The first argument to the non-static Java function 'doubleIntEcho' is not a valid object reference
我想从 XSLT 样式表调用 Java(扩展)方法。
错误信息是:
非静态 Java 函数“doubleIntEcho”的第一个参数不是有效的对象引用。
我的简化 POC 由 4 个部分组成:
1. XML:文件
<?xml version="1.0"?> <value> <amount>1</amount> <amount>2</amount> <amount>3</amount> </value>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jcall="java:xyzXsltCallingJavaEchoTwice"> <xsl:output method="xml"/> <xsl:template match="/value"> <value> <xsl:apply-templates select="amount"/> </value> </xsl:template> <xsl:template match="amount"> <xsl:copy> <value> <xsl:value-of select="text()"/> </value> <value-int> <xsl:value-of select="jcall:doubleIntEcho(number(.))"/> </value-int> <value-string> <xsl:value-of select="jcall:doubleStringEcho(text())"/> </value-string> </xsl:copy> </xsl:template> </xsl:stylesheet>
3.Java类带扩展名:
package x.y.z;
public class XsltCallingJavaEchoTwice {
public static String doubleStringEcho( String input) {
return "Source=" + input + ", echo=" + input + ", echo2=" + input;
}
public static int doubleIntEcho( int input) {
return input + input;
}
}
4. Java 环境启动 XSLT 转换过程:
public class XsltFilterParentWithAllChildren {
private static final String XML_FILENAME = "files/xslt-call-java-amounts.xml";
private static final String XSLT_FILENAME = "files/xslt-call-java-xsl.xsl";
private static final String XML_OUTPUT_FILENAME = "files/output.xml";
public static void main(String[] args) {
deleteFile( XML_OUTPUT_FILENAME);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try (InputStream is = new FileInputStream(XML_FILENAME)) {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
try (FileOutputStream output = new FileOutputStream(XML_OUTPUT_FILENAME)) {
transform(doc, output);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void transform(Document doc, OutputStream output) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(
new StreamSource(new File(XSLT_FILENAME)));
transformer.transform(new DOMSource(doc), new StreamResult(output));
}
private static void deleteFile( String pathname) {
new File( pathname).delete();
}
}
看着https://xalan.apache.org/xalan-j/extensions.html#ext-func-calls我宁愿期待例如xmlns:java="http://xml.apache.org/xalan/java"
和然后例如<xsl:value-of select="java:xyzXsltCallingJavaEchoTwice.doubleIntEcho(number(.))"/>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.