child::* 和 child::node() 之间的 XPath 区别 [英] XPath difference between child::* and child::node()
问题描述
下面两个xpath表达式有区别吗?
child::*
child::node()
我在 this 游乐场页面上尝试了这两种表达方式,得到了相同的结果.
<块引用>child::*
选择当前节点的所有元素子节点
child::node()
选择当前节点的所有子节点
但我不明白.
child::*
和 child::node()
都指向 children 的当前节点,所以区别实际上在于元素和节点.
一个元素是一种节点.
XPath 在其 XML 模型中具有以下节点类型:
- 元素
- 属性
- 文字
- 命名空间
- 处理指令 (PI)
- 评论
- 根
对于您的示例 XML/HTML,
<头><title>我的页面</title>头部><身体><h2>欢迎来到我的<a href="#">页面</a></h2><p>这是第一段</p>.<!-- 到此结束--></html>
有 count(//*)
= 7 个元素和 count(//node())
= 21 个节点.
您的 Playground XPath 是 //h2/a
,它并没有真正说明 child::*
与 child::node()
>.
如果你考虑//h2/*
vs //h2/node()
,那么
//h2/*
选择单个节点,一个元素:page</a>
//h2/node()
选择两个节点,一个text节点和一个元素:欢迎来到我的<a href="#">page</a>
Is there a difference between the following two xpath expressions?
child::*
child::node()
I tried both expressions on this playground page and got the same result.
W3schools says
child::*
Selects all element children of the current node
child::node()
Selects all children of the current node
but I do not get the difference.
Both child::*
and child::node()
refer to the children of the current node, so the difference is really in the difference between that of an element and a node.
An element is a type of node.
XPath has the following node types in its model of XML:
- element
- attribute
- text
- namespace
- processing instruction (PI)
- comment
- root
For your example XML / HTML,
<html>
<head>
<title>My page</title>
</head>
<body>
<h2>Welcome to my <a href="#">page</a></h2>
<p>This is the first paragraph</p>.
<!-- this is the end -->
</body>
</html>
there are count(//*)
= 7 elements and count(//node())
= 21 nodes.
Your playground XPath is //h2/a
, which doesn't really illustrate child::*
vs child::node()
.
If instead you consider //h2/*
vs //h2/node()
, then
//h2/*
selects a single node, an element:<a href="#">page</a>
//h2/node()
selects two nodes, a text node and an element:Welcome to my <a href="#">page</a>
这篇关于child::* 和 child::node() 之间的 XPath 区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!