xml - 关于根节点的简单 XPath 问题

标签 xml xpath

我刚开始学习 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/

相关文章:

c# - XPath 版本搜索

c - 使用 libxml2 的 XPath 在 XML 文档中查找命名空间节点时出现问题

xml - PowerShell XML SelectNodes 无法处理 XPath

c# - 由于单引号,XpathNavigator 给出 System.Xml.XPath.XPathException

用于 xml 列表的 Python 函数

PHP-MYSQL : update mysql from xml file

javascript - 如何使用 Watir 单击具有 Javascript 的链接

c++ - 你如何在 win32 C++ 中写入 xml 文件?

xml - 对前兄弟和后兄弟的影响感到困惑

windows-phone-7 - HtmlAgilityPack XPath 无效 token