java - 从 xslt 调用 java 静态方法时出错

标签 java xml xslt

我正在尝试从 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/

相关文章:

c# - 将内部数据元素映射到外部供应商的 XML 模式

java - 汇总列的逻辑错误 [Java]

java - 如何使用泛型在 Java 中将 List<?> 转换为 List<T>?

java - 使用 XMLUnit 忽略测试 XML 中不存在的所有元素

XSLT - 如果 super 标签包含特定术语,如何忽略 XML 中的子标签

xslt - XSL 命名空间误解

XSLT 深化内容结构

java - 如何对JTable中JComboBox的字符串进行排序?

java - 如何在spring boot中重新加载外部属性文件而不需要重启tomcat?

php - 如何使用 php 访问来自 url 的 xml 输出中的数据点?