java - 无法从 SaxonEE9-9-1-4J 中的 XSLT 调用扩展 Java 方法

标签 java xslt saxon

我有以下简单的 XSLT 示例:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                xmlns:eg="http://example.com/saxon-extension"
                xmlns:eks="ekstern"
                xmlns:dmp="http://arealinformation.miljoeportal.dk/gis/services/distribution/MapServer/WFSServer"
                exclude-result-prefixes="eg">
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:copy>
            <xsl:sequence select="eg:shift-left(1,2)"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

我有这个基本的“ShiftLeft”类,它是我从撒克逊人的例子中得到的:https://www.saxonica.com/html/documentation/extensibility/integratedfunctions/ext-full-J.html

package diadem.base.plugin.helpers;

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.expr.XPathContext;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.IntegerValue;
import net.sf.saxon.value.Int64Value;

public class ShiftLeft extends ExtensionFunctionDefinition {
    @Override
    public StructuredQName getFunctionQName() {
        return new StructuredQName("eg", "http://example.com/saxon-extension", "shift-left");
    }

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

    @Override
    public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
        return SequenceType.SINGLE_INTEGER;
    }

    @Override
    public ExtensionFunctionCall makeCallExpression() {
        return new ExtensionFunctionCall() {
            @Override
            public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
                long v0 = ((IntegerValue)arguments[0]).longValue();
                long v1 = ((IntegerValue)arguments[1]).longValue();
                long result = v0<<v1;
                return Int64Value.makeIntegerValue(result);
            }
        };
    }
}

由于某种原因,我在 XSLT 中遇到以下错误消息:

Static error in {eg:shift-left(1,2)} in expression in xsl:sequence/@select on line 12 column 56 of conflictsTestTransform.xslt:
  XPST0017: Cannot find a 2-argument function named Q{http://example.com/saxon-extension}shift-left()

我真的不知道为什么它不能调用 Java 扩展函数中的“Call”方法。我用于转换的代码:

 private static Document transform(Document inputDoc, File xsltFile) throws XSLTransformException {
        JDOMSource source = new JDOMSource(inputDoc, null)
        JDOMResult result = new JDOMResult()
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance("com.saxonica.config.EnterpriseTransformerFactory", null)
            tFactory.setURIResolver(new XsltUriResolver())
            if(tFactory instanceof TransformerFactoryImpl) {
                TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) tFactory;
                Configuration saxonConfig = tFactoryImpl.getConfiguration();
                saxonConfig.registerExtensionFunction(new ShiftLeft());
            }

            Templates templates = TransformerFactory.newInstance("com.saxonica.config.EnterpriseTransformerFactory", null).newTemplates(new StreamSource(xsltFile))
            Transformer transformer = templates.newTransformer()
            result.setFactory(null)  // null ok
            try {
                transformer.transform(source, result)
                return result.getDocument()
            }
            catch (TransformerException e) {
                throw new XSLTransformException("Could not perform transformation", e)
            }
        } catch (TransformerConfigurationException tce) {
            // Error generated by the parser
            // log.warn("Transformer Factory error", tce)
        } catch (SAXException sxe) {
            // Error generated by this application
            // (or a parser-initialization error)
            // log.warn("SAXException", sxe)
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            // log.warn("Parser build error", pce)
        } catch (IOException ioe) {
            // I/O error
            // log.warn("IO error", ioe)
        }

        return null
    }

最佳答案

您有两个 TransformerFactories 和两个 Saxon 配置;您已经向其中一个注册了扩展功能,并且您正试图在另一个中使用它。

关于java - 无法从 SaxonEE9-9-1-4J 中的 XSLT 调用扩展 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187644/

相关文章:

Java 数组问题

xml - 连接几个节点 xsl

xslt - 在结果文档中生成空/空白命名空间声明

java - 是否可以创建 XSL 转换器输出流?

java - 通过增加值排序和显示 TreeMap 问题

java - 安卓卡牌游戏动画

C# Parallel.ForEach XslCompiledTransform 与 Saxon 9.7.0.6 HE

xslt-2.0 - 为什么 Saxon 将结果文档 URI 评估为相同?

java - Java 中的证书路径验证,包括 CRL 验证

xml - 选择处理指令的 XPath 表达式是什么?