XPath: Recursive Ancestor Search

2019-07-31 16:36发布

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条回答
在下西门庆
2楼-- · 2019-07-31 17:23

(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.

查看更多
登录 后发表回答