简体   繁体   English

Saxon-HE Java 扩展功能(配置问题)

[英]Saxon-HE Java Extension Functions (Problem configuring)

I currently use Saxon9 open source version with extensions written in Java.我目前使用的是 Saxon9 开源版本,扩展名为 Java。 I am trying to migrate to SaxonHE and I have read the documentation and examples shown here.我正在尝试迁移到 SaxonHE,并且我已阅读此处显示的文档和示例。

Java extension functions: full interface Java 扩展功能:全接口

and

Saxon Configuration File 撒克逊配置文件

When I try and execute my XSLT transformation I get errors like this when it encounters one of my external java functions.当我尝试执行我的 XSLT 转换时,当它遇到我的外部 java 函数之一时,会出现这样的错误。

  XPST0017: Cannot find a 2-argument function named
  Q{http://com.commander4j.Transformation.XSLT_Ext_NVL}nvl()

So here is what I've done so far.所以这是我到目前为止所做的。

My java extension function is written like this.我的 java 扩展 function 是这样写的。


import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.StringValue;

public class XSLT_Ext_NVL extends ExtensionFunctionDefinition
{

    @Override
    public SequenceType[] getArgumentTypes()
    {
        return new SequenceType[]{SequenceType.SINGLE_STRING, SequenceType.SINGLE_STRING};
    }

    @Override
    public StructuredQName getFunctionQName()
    {
        return new StructuredQName("c4j_XSLT_Ext_NVL", "http://com.commander4j.Transformation.XSLT_Ext_NVL", "nvl");
    }

    @Override
    public SequenceType getResultType(SequenceType[] arg0)
    {
         return SequenceType.SINGLE_STRING;
    }

    @Override
    public ExtensionFunctionCall makeCallExpression()
    {
           return new ExtensionFunctionCall() {
                @Override
                public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
                    String value = ((StringValue)arguments[0]).getStringValue();
                    String defaultValue = ((StringValue)arguments[1]).getStringValue();
                    
                    String result = "";

                    if (value == null)
                    {
                        value = "";
                    }

                    result = value;

                    if (result.equals(""))
                    {
                        result = defaultValue;
                    }

                    return StringValue.makeStringValue(result);
                }
            };
    }

}

I have created a Saxon configuration file which looks like this.我创建了一个看起来像这样的撒克逊配置文件。 My example looks a little different than the example on the Saxon website as that example includes the function name after the class name separated by a $ - when I tried it I got an error message that Saxon could not find the class.我的示例看起来与 Saxon 网站上的示例略有不同,因为该示例在 class 名称之后包含 function 名称,以 $ 分隔 - 当我尝试它时,我收到一条错误消息,指出 Saxon 找不到 ZA2F2ED4F8EBC2CBBDZC21。

    edition="HE"
    licenseFileLocation=""
    label="c4jMiddleware">

    <resources>
        <extensionFunction>com.commander4j.Transformation.XSLT_Ext_NVL</extensionFunction>
    </resources>
    
</configuration>

I am loading Configuration using this syntax.我正在使用此语法加载配置。

Source xmlSource = new StreamSource(new File(System.getProperty("user.dir") + File.separator + "xml" + File.separator + "config" + File.separator +"SaxonConfiguration.xml"));
                                Configuration.readConfiguration(xmlSource);

Below is an extract from my XSLT which tries to invoke the java function.下面是我的 XSLT 的摘录,它试图调用 java function。

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:c4j="http://www.commander4j.com" 
    xmlns:c4j_XSLT_Ext_NVL="http://com.commander4j.Transformation.XSLT_Ext_NVL" 
    xmlns:c4j_XSLT_Ext="http://com.commander4j.Transformation.XSLTExtension"
    exclude-result-prefixes="xs c4j c4j_XSLT_Ext" version="2.0">

    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
    <xsl:strip-space elements="*"/>
    .
    .
    .
    <xsl:template match="xml">

        <xsl:param name="pack_conv" select="c4j_XSLT_Ext_NVL:nvl($pack_conv_temp, '1')"/>

If someone could give me a clue as to where I've gone wrong it would be most appreciated.如果有人能给我一个关于我哪里出错的线索,那将不胜感激。

Dave戴夫

As Martin suggests, the most likely explanation is that you are not actually using the configuration you have created by reading the configuration file.正如 Martin 所建议的,最可能的解释是您实际上并没有使用通过阅读配置文件创建的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM