簡體   English   中英

從xslt調用Java靜態方法時出錯

[英]Error when calling java static method from 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:“非靜態Java函數'getCountryCode'的第一個參數不是有效的對象引用。
46860 [main]錯誤-無法通過xslt javax.xml.transform.TransformerConfigurationException轉換收到的消息:無法在com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java: 828),位於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配合使用對我來說很好

<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很好地轉換此示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM