我正在尝试从 XSLT 调用静态 java 代码。我的 XSLT 文件如下所示
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="interface.dev"
exclude-result-prefixes="ns1"
xmlns:xltxe="http://www.ibm.com/xmlns/prod/xltxe-j"
xmlns:countryCode="http://com.abc/common/utils">
<xltxe:java-extension prefix="countryCode" class="com.abc.common.utils.CountryStaticInfo"/>
<xsl:variable name="var" select="ns1:parties/ns1:party[@code='BNE']/ns1:country"/>
<xsl:template match="/ns1:import_lc_iss">
<html>
<body>
<table border="1">
<tr>
<xsl:value-of select="countryCode:getCountryCode($var)" />
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我的 Java 代码如下所示:
public synchronized static String getCountryCode(String aS_ctryCode) throws Exception, StaticInfoException {
ArrayList lAL_entities = com.abc.services.JavaProgramContext.getServerEntities();
return getCountryCode(lAL_entities.get(0).toString(),aS_ctryCode);
}
我收到以下错误
RROR: 'The first argument to the non-static Java function 'getCountryCode' is not a valid object reference.
46860 [main] ERROR - Cannot transform the recieved message via xslt javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:828) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:617)
第一个修正案:带有 xalan 支持的新 xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="interface.dev"
exclude-result-prefixes="ns1"
xmlns:xalan="http://xml.apache.org/xalan"
extension-element-prefixes="xalan"
xmlns:java="java"
xmlns:util="com.abc.common.utils.CountryStaticInfo">
<xltxe:java-extension prefix="countryCode" class="com.abc.common.utils.CountryStaticInfo"/>
<xsl:variable name="var" select="AU"/>
<xsl:template match="/ns1:import_lc_iss">
<html>
<body>
<table border="1">
<tr>
<xsl:variable name="new-pop" select="com.abc.common.utils.CountryStaticInfo.getCountryCode(string(@var))"/>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
最佳答案
这是一个遵循 http://xml.apache.org/xalan-j/extensions.html#ext-functions 中的文档和示例的示例对于我来说,Oracle JRE 8 工作得很好,XSLT 是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:java="http://xml.apache.org/xalan/java" exclude-result-prefixes="java">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>sheet1.xsl</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="country-list">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="country">
<li>
<xsl:value-of select="java:org.example.MyClass.getCountryCode(.)"/>
</li>
</xsl:template>
</xsl:stylesheet>
Java类库在包org.example
中有
package org.example;
import java.util.HashMap;
import java.util.Map;
public class MyClass {
private static final Map<String, String> COUNTRY_LIST = new HashMap<>();
static {
COUNTRY_LIST.put("Spain", "es");
COUNTRY_LIST.put("USA", "us");
}
public static String getCountryCode(String name) {
return COUNTRY_LIST.get(name);
}
}
然后 JRE 中的内置 Transformer
可以很好地转换此示例。
关于java - 从 xslt 调用 java 静态方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33799564/