XPath: Recursive Ancestor Search

这一生的挚爱 提交于 2019-12-11 06:06:34

问题


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?


回答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.



来源:https://stackoverflow.com/questions/1362466/xpath-recursive-ancestor-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!