XPath: Finding a Recursive Ancestor

When selecting the current current node, I want to find the <name> [./name] and return the text content. If the <name> element does not exist in the currently selected node, I want to check the parent element [./parent::name] and so on recursively to the root, returning the value of the nearest parent where this element exists.

Can this be done using XPath?

+1
xpath parent ancestor
Sep 01 '09 at 13:22
source share
1 answer

(Edit: I misinterpreted the question for the first time)

I suggest using

 ancestor-or-self::name[1] 

This finds all name elements, starting with self, parent, etc., and arranges them at a distance from itself. Therefore, choosing [1] gives you the closest.

+6
Sep 01 '09 at 13:28
source share



All Articles