Using XPath is it possible to get the name of each node in a path to a target node as a String?
Example.xml
<parent>
<childOne>
<target>true</target>
</childOne>
<childTwo>
<target>true</target>
</childTwo>
<childThree>
<target>false</target>
</childThree>
</parent>
Selects all ancestors where target is true
//node()[target = "true"]/ancestor-or-self::*
Is it possible to get this result
"parent/childOne/target"
"parent/childTwo/target"
How about this XPath 2.0 expression:
This will return the path strings seperated by newline characters. If you want seperate result nodes for each path you may want to use
instead.
Note that I had to change
target = 'true'
intotext() = 'true'
to include the tags at the lowest level.The trick with the concatenation of the partial path strings was inspired by this answer: Concatenate multiple node values in xpath