XPath:递归祖先搜索

发布于 2024-08-03 23:56:42 字数 208 浏览 8 评论 0原文

选择当前节点后,我希望找到一个元素 [./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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-08-10 23:56:42

(编辑:我第一次误解了这个问题)

我建议使用

ancestor-or-self::name[1]

This 查找所有 name 元素,从 self、parent 等开始,并以与 self 的距离递增的方式对它们进行排序。因此,选择 [1] 将为您提供最近的一个。

(Edit: I misinterpreted the question the first time)

I propose to use

ancestor-or-self::name[1]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文