的缩写谁能解释什么是的XPath - position()函数
/root/a[position()=1 or position()=2
和
/root/a[1 or 2]
之间的区别? 我假设第二个是第一个的缩写形式,但是Java XPath(Sun JDK 1.6.0)处理器认为不然。以下是我的测试应用程序。
libxml2库和db2 XPath处理器也认为这些路径不同。所以它看起来不像JDK的bug。
import java.io.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
public class XPathTest {
public static void main(String[] args) throws Exception {
//String xpathStr = "/root/a[position()=1 or position()=2]";
String xpathStr = "/root/a[1 or 2]";
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
Reader irdr = new StringReader(
"<root><a name=\"first\"/><a name=\"second\"/><a name=\"third\"/></root>");
InputSource isrc = new InputSource(irdr);
XPathExpression expr = xp.compile(xpathStr);
Object result = expr.evaluate(isrc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Element element = (Element) node;
System.out.print(element.getNodeName() + " " + element.getAttributeNode("name"));
System.out.println();
}
}
}
我猜@安顿不会回来,但如果他确实应该将[生锈的答案](http://stackoverflow.com/a/7525994/539490)标记为正确 – AJP