Match text with XPATH syntax

Wyck picture Wyck · Jan 28, 2011 · Viewed 8.4k times · Source

Exmple feed: view-source:http://rss.packetstormsecurity.org/files/tags/exploit/

I only want to return sections of xml where the parent title node has matching text in it, in this example the text to match is "site".

 //get feed with curl

 $doc = new SimpleXmlElement($xml, LIBXML_NOCDATA);

 //$result = $doc->xpath('//title'); //this works returns all the <title>'s

 $result = $doc->xpath('//title[site]');                     //doesn't work
 $result = $doc->xpath('//title[text()="site"]');           //doesn't work
 $result = $doc->xpath('//title[contains(site)]');           //doesn't work
 $result = $doc->xpath('//title[contains(text(),'Site')]');  //doesn't work


 foreach ($result as $title)
 echo "$title<br />"

Answer

lonesomeday picture lonesomeday · Jan 28, 2011

The call you need is, I think, this:

$result = $xpath->query('//title[contains(.,"Site")]');

Note that this is case-sensitive.

Note that the contains XPath function takes two arguments: a haystack and a needle. In this case, we are using the current node's text value as the haystack, which is indicated by the use of the dot (.).