以下两个 xpath 表达式之间有区别吗?
child::*
child::node()
我在 this 上尝试了这两种表达方式游乐场页面并得到相同的结果。
W3schools说
child::*
Selects all element children of the current node
child::node()
Selects all children of the current node
但我不明白其中的区别。
最佳答案
两者 child::*
和 child::node()
指的是当前节点的子节点,所以区别真的在于的区别元素 和一个 节点 .
元素是一种节点。
XPath 在其 XML 模型中具有以下节点类型:
对于您的示例 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>
有
count(//*)
= 7 个元素和 count(//node())
= 21 个节点。你的游乐场 XPath 是
//h2/a
,这并没有真正说明 child::*
对比 child::node()
.如果您考虑
//h2/*
对比 //h2/node()
, 然后//h2/*
选择单个节点,一个元素:<a href="#">page</a>
//h2/node()
选择两个节点,一个文本节点和一个元素:Welcome to my
<a href="#">page</a>
关于xml - child::* 和 child::node() 之间的 XPath 区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045856/