tags:

views:

37

answers:

3

I've this xml

<root>
    <node1>
        <node2>xxx</node2>
    </node1>
    ...
    <node1>
        <node2>yyy ABC yyy</node2>
    </node1>
    ...
    <node1>
        <node2>zzz</node2>
    </node1>
</root>

I want to get node1 that has a node2 containing the text ABC.
Is it possible to achieve this using XPath?

A: 

//node1[node2[contains(text(),"ABC")]]

Marcelo Cantos
This only works when the text is exactly "ABC". Jweede's answer will find any nodes which contain "ABC" in its text.
Greg
+4  A: 

I'm pretty sure this will do the trick:

node1[node2[contains(text(),"ABC")]]

EDIT: Tested, seems to work.

Jweede
A: 

I tried using this XPath Tester here: http://www.shrinkrays.net/tools/xpath-tester.aspx. Does this work for you?

//node1/node2[contains(.,"ABC")]
awshepard
this will select node2 instead of node1.
Jweede