使用SAXON

时间:2015-11-19 11:55:20

标签: saxon xpath-2.0

我想找到一个有效的SAXON示例来执行一个简单的任务,比如评估以下XPATH 2.0表达式:

abs(-a)

评估像

这样的表达式
3 + 4

工作得很好,但我没有设法为变量 a 提供SAXON值。

我试过像:

    final Processor proc = new Processor(false);
    final XPathCompiler xpath = proc.newXPathCompiler();
    QName a = new QName("a");
    xpath.declareVariable(a);

    System.out.println(xpath.evaluate("abs(-a)", null));

但显然这不起作用,因为我没有为 a 提供价值。

如何为 a 声明一个值?

任何帮助都将受到高度赞赏!

编辑:这不起作用:

final Processor proc = new Processor(false);
final XPathCompiler xpath = proc.newXPathCompiler();

XPathSelector exp = xpath.compile("abs(-$a)").load();

exp.setVariable(new QName("a"), new XdmAtomicValue(17));
XdmItem result = exp.evaluateSingle();

这会引发异常:

  

net.sf.saxon.s9api.SaxonApiException:XPath表达式中未声明的变量:$ a       at net.sf.saxon.s9api.XPathCompiler.internalCompile(XPathCompiler.java:500)       at net.sf.saxon.s9api.XPathCompiler.compile(XPathCompiler.java:472)       在xpathtest.SaxonTest.test(SaxonTest.java:26)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:483)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)       在org.junit.runners.ParentRunner.run(ParentRunner.java:309)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:net.sf.saxon.trans.XPathException:XPath表达式中未声明的变量:$ a       at net.sf.saxon.sxpath.IndependentContext.bindVariable(IndependentContext.java:458)       at net.sf.saxon.expr.parser.XPathParser.parseVariableReference(XPathParser.java:1905)       在net.sf.saxon.expr.parser.XPathParser.parseBasicStep(XPathParser.java:1703)       at net.sf.saxon.expr.parser.XPathParser.parseStepExpression(XPathParser.java:1638)       at net.sf.saxon.expr.parser.XPathParser.parseRelativePath(XPathParser.java:1564)       at net.sf.saxon.expr.parser.XPathParser.parsePathExpression(XPathParser.java:1529)       at net.sf.saxon.expr.parser.XPathParser.parseSimpleMappingExpression(XPathParser.java:1541)       at net.sf.saxon.expr.parser.XPathParser.parseUnaryExpression(XPathParser.java:1419)       在net.sf.saxon.expr.parser.XPathParser.parseUnaryExpression(XPathParser.java:1390)       at net.sf.saxon.expr.parser.XPathParser.parseExprSingle(XPathParser.java:511)       at net.sf.saxon.expr.parser.XPathParser.parseFunctionArgument(XPathParser.java:2629)       在net.sf.saxon.expr.parser.XPathParser.parseFunctionCall(XPathParser.java:2371)       at net.sf.saxon.expr.parser.XPathParser.parseBasicStep(XPathParser.java:1723)       at net.sf.saxon.expr.parser.XPathParser.parseStepExpression(XPathParser.java:1638)       at net.sf.saxon.expr.parser.XPathParser.parseRelativePath(XPathParser.java:1564)       at net.sf.saxon.expr.parser.XPathParser.parsePathExpression(XPathParser.java:1529)       at net.sf.saxon.expr.parser.XPathParser.parseSimpleMappingExpression(XPathParser.java:1541)       at net.sf.saxon.expr.parser.XPathParser.parseUnaryExpression(XPathParser.java:1419)       at net.sf.saxon.expr.parser.XPathParser.parseExprSingle(XPathParser.java:511)       在net.sf.saxon.expr.parser.XPathParser.parseExpression(XPathParser.java:450)       在net.sf.saxon.expr.parser.XPathParser.parse(XPathParser.java:386)       at net.sf.saxon.expr.parser.ExpressionTool.make(ExpressionTool.java:102)       在net.sf.saxon.sxpath.XPathEvaluator.createExpression(XPathEvaluator.java:153)       at net.sf.saxon.s9api.XPathCompiler.internalCompile(XPathCompiler.java:497)       ......还有25个

2 个答案:

答案 0 :(得分:1)

首先,它应该是abs(-$a)。请注意$

要提供参数,您不能使用“捷径”方法XPathCompiler.evaluate()。你必须做很多事情:

XPathSelector exp = xpath.compile("abs(-$a)").load();
exp.setVariable(new QName("a"), new XdmAtomicValue(17));
XdmItem result = exp.evaluateSingle();

答案 1 :(得分:1)

我知道这个帖子已经老了,但我最近遇到了这个问题,花了将近2个小时寻找答案。最后,我使用了Saxon-HE 9.4。尽管声明了外部变量,我仍然从编译中得到错误,直到我也调用:

    xpath.setAllowUndeclaredVariables(true);