使用saxon s9api的扩展功能

时间:2017-01-28 02:47:52

标签: saxon

我正在尝试添加扩展功能,但失败了:

Caused by: net.sf.saxon.trans.XPathException: Unknown system function follow()
    at net.sf.saxon.expr.parser.XPathParser.grumble(XPathParser.java:282)

我看到(在调试中)该功能在集成库中注册。我期待撒克逊人在集成库中寻找函数,但它正在搜索系统函数并抛出错误。是什么导致此函数表示为系统函数。 我使用以下内容:

<dependency>
   <groupId>net.sf.saxon</groupId>
   <artifactId>Saxon-HE</artifactId>
   <version>9.7.0-14</version>
</dependency>

谢谢

我的代码是

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.s9api.Processor;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.SequenceType;

public class FollowTest {
    public static void main(String[] args) throws Exception {
        new FollowTest().test();
    }

    private void test () throws Exception {
        Processor proc = new Processor(false);
        proc.registerExtensionFunction(new Follow());
        XPathCompiler xx = proc.newXPathCompiler();
        XPathExecutable x = xx.compile("follow(/a/b/c)/type='xyz'");
    }

    public class Follow extends ExtensionFunctionDefinition {

        @Override
        public StructuredQName getFunctionQName() {
            return new StructuredQName("", "http://example.com/saxon-extension", "follow");
        }

        @Override
        public int getMinimumNumberOfArguments() {
            return 1;
        }

        @Override
        public int getMaximumNumberOfArguments() {
            return 1;
        }

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

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

        @Override
        public boolean trustResultType() {
            return true;
        }

        @Override
        public boolean dependsOnFocus() {
            return false;
        }

        @Override
        public boolean hasSideEffects() {
            return false;
        }

        @Override
        public ExtensionFunctionCall makeCallExpression() {
            return null;
        }
        private class followCall extends ExtensionFunctionCall {
            @Override
            public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
                return null;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在你编写的XPath表达式中

follow(/a/b/c)

假定没有名称空间前缀的函数名称位于函数的默认名称空间中,默认情况下是系统函数名称空间http://www.w3.org/2005/xpath-functions。您需要使用绑定到扩展函数定义中出现的URI的前缀,即http://example.com/saxon-extension