Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match text with XPATH syntax

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 />"
like image 503
Wyck Avatar asked Jan 27 '11 23:01

Wyck


People also ask

How do I search for text in XPath?

So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));

Can we use text () in XPath?

5) XPath Text() Function The XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to find the exact text elements and it locates the elements within the set of text nodes. The elements to be located should be in string form.

Which is correct XPath syntax?

Syntax of XPath //: Used to select the current node. tagname: Name of the tag of a particular node. @: Used to select the select attribute. Attribute: Name of the attribute of the node.

How will you write XPath if the tag has only text?

We will start to write XPath with //, followed by input tag, then we will use the select attribute, followed by its attribute name like name, id, etc, and then we will choose a value of attribute in single quotes. Here, (1 of 1) means exact match. It indicates that there is only one element available for this XPath.


1 Answers

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 (.).

like image 187
lonesomeday Avatar answered Oct 08 '22 18:10

lonesomeday