获取 XPath 中的父元素名称
我试图弄清楚如何从文本节点的范围中获取父节点的名称。
//text()[name(parent)='p']
如何获取当前节点父节点的名称?
I am trying to figure out how to get the name of the parent from a text node's scope.
//text()[name(parent)='p']
How can you get the name of the current node's parent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试测试该名称,那么您几乎已经完成了:
如果您尝试返回该名称:
If you're trying to test the name, you almost had it:
If you're trying to return the name:
仅供参考,术语点:文本节点不是元素。
无论如何,选择当前节点的父节点的最简洁的方法是
..
因此,当前节点的父元素的名称(可以是文本节点)是
name( ..)
将其替换为您的 XPath 表达式:
但是一种不太迂回的编写方法是
(假设文档中的
p
元素没有名称空间前缀)。无论哪种方式,您都会选择属于名为p
的元素子级的所有文本节点。FYI, point of terminology: a text node is not an element.
Anyway, the most succinct way to select the parent of the current node is
..
So, the name of the parent element of the current node (which could be a text node) is
name(..)
Substituting that into your XPath expression:
But a less roundabout way to write that would be
(assuming the
p
elements in the document have no namespace prefix). Either way, you're selecting all text nodes that are children of elements namedp
.只要父节点的
name
属性为p
,这将获取
节点的所有父节点。This will get all parents of
<text>
nodes as long as the parent has aname
attribute ofp
.