gpt4 book ai didi

xml - 为什么 XPath 的节点测试 node() 不返回属性节点?

转载 作者:行者123 更新时间:2023-12-03 16:23:33 24 4
gpt4 key购买 nike

我一直在查看 XPath 的节点测试 (documentation here) 并在一些示例 xml 上测试它们。

<apartment residents="4" shared="true"> 
fries
<!-- potatosalad -->
</apartment>

虽然属性的 Kindtest//apartment/@* 将返回居民和共享的意思,它们都是公寓节点的 child ,但任何种类的 Kindtest//apartment/nodes() 将返回文本节点 fries 和评论节点土 bean 沙拉 但不是属性节点居民或共享 .

这对我来说似乎违反直觉,我想知道:这有充分的理由吗?

最佳答案

XPath //apartment/node()将选择所有 apartment元素的 child 节点,它们是 * , processing-instruction() , comment() , 和 text() .
属性和命名空间的特殊之处在于它们的父元素是它们“附加到”的元素,但它们不被视为 child 节点,并且不会从 中选择 child 轴。
规范中有关子轴的信息(我添加了粗体以强调):
https://www.w3.org/TR/2017/REC-xpath-31-20170321/#axes

The child axis contains the children of the context node, which are the nodes returned by the Section 5.3 children Accessor.

Note: Only document nodes and element nodes have children. If the context node is any other kind of node, or if the context node is an empty document or element node, then the child axis is an empty sequence. The children of a document node or element node may be element, processing instruction, comment, or text nodes. Attribute, namespace, and document nodes can never appear as children.



https://www.w3.org/TR/xpath-datamodel-31/#ElementNodeOverview
  1. Exclusive of Attribute and Namespace Nodes, if a node N has a parent element E, then N must be among the children of E. (Attribute and Namespace Nodes have a parent, but they do not appear among the children of their parent.)

关于xml - 为什么 XPath 的节点测试 node() 不返回属性节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com