Non-obvious XPath: Getting an element by specifying it's value

This one took around half - an - hour out of my working day.
Problem:
I needed to get a specific element from an XML document based on its value / element content.
[Root]
[SomeSection]
[path]C:\Diablo II\[/path]
[path]H.G.Wells[/path]
[/SomeSection]
[Section2]
[path]Morlocks[/path]
[/Section2]
[/Root]

So I need a query to get me the element which has the text H.G.Wells. I could find a whole lot of examples on the net to get me an element based on the value of one of it's attribute.
e.g. /root/SomeSection[@attr='value']
or based on one of its sub elements
e.g. /root/SomeSection[path='H.G.Wells']
I tried a whole of things but to no avail. Then I turned to pester one of my buddies Ranjeet here, who turned to a saved ref document.

The answer here is
/root/SomeSection/path[.='H.G.Wells']
the dot was my missing link to the solution. Oh well, what the hell...
Here's hoping the next guy finds it sooner

No comments:

Post a Comment