Error when calling java static method from xslt

2019-05-20 00:06发布

I'm trying to call from XSLT a static java code. My XSLT file looks as the following

<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>

My Java code looks like that:

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);
}

I get the following error

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)

1st amendment: the new xslt with xalan support

    <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>

标签: java xml xslt
1条回答
Evening l夕情丶
2楼-- · 2019-05-20 00:18

Here is an example following the documentation and examples in http://xml.apache.org/xalan-j/extensions.html#ext-functions that works fine for me with Oracle JRE 8, the XSLT is

<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>

the Java class library has in the package org.example has

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);
    }
}

and then the built-in Transformer in the JRE transforms this example fine.

查看更多
登录 后发表回答