以下代码返回4个节点(使用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节点:
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();我做错了什么?
更新:看起来它需要一个$标志:
compile("/windward-studios/Employees/Employee[@EmployeeID < $p1]");对吗?
发布于 2020-08-04 23:25:14
XPath和XSLT中的变量引用从$开始,因此$p1是XPath中引用名为p1的变量的正确方式。您以前的尝试p1将选择一个名为p1的元素节点。
https://stackoverflow.com/questions/63253546
复制相似问题