XPath:递归祖先搜索
选择当前节点后,我希望找到一个元素 [./name]
并返回文本内容。如果当前选定的节点中不存在元素
我希望递归地检查父元素 [./parent::name]
等等根,返回该元素所在的最近父级的值。
可以用XPath来完成吗?
With my current node selected I wish to find an element <name>
[./name]
and return the text content. If an element <name>
does not exist in the currently selected node I wish to check the parent element [./parent::name]
and so on recursively up to the root, returning the value of the nearest parent where the element exists.
Can it be done with XPath?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(编辑:我第一次误解了这个问题)
我建议使用
This 查找所有
name
元素,从 self、parent 等开始,并以与 self 的距离递增的方式对它们进行排序。因此,选择[1]
将为您提供最近的一个。(Edit: I misinterpreted the question the first time)
I propose to use
This finds all
name
elements, starting with self, parent, and so on, and orders them in increasing distance from self. So selecting[1]
gives you the nearest one.