Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath with multiple contains on different elements

Is it possible to have XPath expression with multiple contains of different element values?

XML

<data>
   <person>
      <firstname>Kerry</firstname>
      <lastname>Packer</lastname>
      <address>Crown</address>
   <person>
   <person>
      <firstname>Kerry</firstname>
      <lastname>Murdoch</lastname>
      <address>California</address>
   <person>
<data>

PHP

$xml = simplexml_load_string($data);
$elements = $xml->xpath("(//person)[firstname[contains(., 'Kerr')]] and [lastname[contains(., 'och')]]");

Currently above XPath expression is flagged as invalid. However if I use it with one element,

$xml->xpath("(//person)[firstname[contains(., 'Kerr')]]"); 

then it works fine.

like image 536
TeaCupApp Avatar asked Aug 31 '13 11:08

TeaCupApp


1 Answers

You simply want

//person[contains(firstname, 'Kerr') and contains(lastname, 'och')]
like image 86
Martin Honnen Avatar answered Oct 26 '22 03:10

Martin Honnen