我刚开始学习 XPath,我正在学习没有缩写的语言。
When a location path starts with the character '/' the initial context node is forces to be the root node...
书中的示例 xml:
<widget type="gadget">
...
</widget>
如果我应该编写 XPath 以便获得类型属性,为什么我必须编写:
/ child ::小部件/属性::类型
而不是这样的/attribute::type
我对根节点有什么误解吗?因为我认为 '/' 和小部件是一样的......
谢谢
最佳答案
来自 http://www.w3.org/TR/xpath/#attribute-nodes
Each element node has an associated set of attribute nodes; the element is the parent of each of these attribute nodes; however, an attribute node is not a child of its parent element.
只有元素节点才有属性。
根节点不是元素。来自 http://www.w3.org/TR/xpath/#root-node
The root node is the root of the tree. A root node does not occur except as the root of the tree. The element node for the document element is a child of the root node. The root node also has as children processing instruction and comment nodes for processing instructions and comments that occur in the prolog and after the end of the document element.
因此您不能像在/@*
或/attribute::*
中那样选择根节点的属性
你问:
Have I misunderstood something about the root node or?
是的。您不应将根节点(一种抽象)与文档元素(有时也称为元素根)混淆
关于xml - 关于根节点的简单 XPath 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5571664/