public class Test {
// create an XPathFactory
XPathFactory xFactory = XPathFactory.newInstance();
// create an XPath object
XPath xpath = xFactory.newXPath();
...
String sample = "<node>"
+ "<child2><page>555</page></child2>"
+ "<child1>"
+ "<page>655</page>"
+ "</child1>"
+ "</node>";
...
Element child1 = (Element)root.getElementsByTagName("child1").item(0);
page = (String)xpath.evaluate("//page", child1,XPathConstants.STRING);
vs
page = (String)xpath.evaluate(".//page", child1,XPathConstants.STRING);
一个人将返回555,另一个将返回655。如果我使用child1元素评估,它将如何在根上评估。我误以为所有评估都将在Child的指定上下文中进行评估。
a"/"或"//"路径开头意味着选择是从包含上下文项目的树的根中。