XPath:递归祖先搜索 [英] XPath: Recursive Ancestor Search

查看:50
本文介绍了XPath:递归祖先搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择当前节点后,我希望找到一个元素 [./name] 并返回文本内容.如果当前选择的节点中不存在元素 我希望检查父元素 [./parent::name] 等等递归直到根,返回元素所在的最近父元素的值.

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.

可以用 XPath 完成吗?

Can it be done with XPath?

推荐答案

(我第一次误解了问题)

( I misinterpreted the question the first time)

我建议使用

ancestor-or-self::name[1]

这将查找所有 name 元素,从 self、parent 等开始,并按照与 self 的距离递增对它们进行排序.所以选择 [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.

这篇关于XPath:递归祖先搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆