如何在 Java 上的 Saxon 中的 XPath 查询中设置变量 [英] How do I set a variable in an XPath query in Saxon on Java
本文介绍了如何在 Java 上的 Saxon 中的 XPath 查询中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码返回 4 个节点(使用 southwind.xml &
The following code returns 4 nodes (using southwind.xml & southwind.xsd):
XPathExecutable exec = datasource.getxPathCompiler().compile("/windward-studios/Employees/Employee[@EmployeeID < 5]");
XPathSelector selector = exec.load();
selector.setContextItem(datasource.getXmlRootNode());
XdmValue nodeSet = selector.evaluate();
但以下返回 0 个节点:
But the following returns 0 nodes:
datasource.getxPathCompiler().declareVariable(new QName("p1"));
XPathExecutable exec = datasource.getxPathCompiler().compile("/windward-studios/Employees/Employee[@EmployeeID < p1]");
XPathSelector selector = exec.load();
selector.setContextItem(datasource.getXmlRootNode());
selector.setVariable(new QName("p1"), new XdmAtomicValue(5));
XdmValue nodeSet = selector.evaluate();
我做错了什么?
更新:看起来它需要一个 $ 符号:
Update: It looks like it needs a $ sign:
compile("/windward-studios/Employees/Employee[@EmployeeID < $p1]");
正确吗?
推荐答案
XPath 和 XSLT 中的变量引用以 $
开头,所以 $p1
是 XPath 中的正确方式引用名为 p1
的变量.您之前的尝试 p1
将选择一个名为 p1
的元素节点.
Variable references in XPath and XSLT start with $
, so $p1
is the right way in XPath to reference the variable named p1
. Your previous attempt p1
would select an element node named p1
.
这篇关于如何在 Java 上的 Saxon 中的 XPath 查询中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文