Vjy
Vjy

Reputation: 2135

xpath to get Node containing text

I tried to search for nodes containing text 'Yahoo' under '/doc/story/content', it returns 'content' node, but I need exact text node that contains 'Yahoo' or it's parent

<doc>
    <story>
        <content id="201009281450332423">
            <ul>MSW NYNES NYPG1 DILMA</ul>
            <p> <k> Yahoo, made </k> it nice </p>
            <p>
               <author>-v-</author>
            </p>
        </content>
    </story>
</doc>

Xpath: "/doc/story/content[contains(., 'Yahoo')]"

Upvotes: 48

Views: 83790

Answers (2)

Ravish
Ravish

Reputation: 2447

Since you need all textNodes only which contain the text Yahoo, use the following XPath.

//text()[contains(., 'Yahoo')]

This should return you all the textNodes only which contains Yahoo (case-sensitive) in it.

Upvotes: 60

Jon
Jon

Reputation: 437854

Your XML is malformed. </content></doc></story> should be </content></story></doc>.

Apart from that, the XPath you would want is

/doc/story/content//*[contains(., 'Yahoo')]

(select any descendant of <content> which contains the text "Yahoo" -- this will select the <p>)

Upvotes: 42

Related Questions