Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : SimpleXML functions : SimpleXMLElement->xpath()


Runs XPath query on XML data ()

Example 2260. Xpath

= <<<XML

$xml = new SimpleXMLElement($string);

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

while(list( ,
$node) = each($result)) {
'/a/b/c: ',$node,"\n";

/* Relative paths also work... */
$result = $xml->xpath('b/c');

while(list( ,
$node) = each($result)) {
'b/c: ',$node,"\n";

The above example will output:

/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff

Notice that the two results are equal.

Related Examples ( Source code ) » simplexml element xpath

Code Examples / Notes » simplexml element xpath


xpath() can also be used to select elements by their attributes. For a good XPath reference check out:
$string = <<<XML
   <size label="Square" width="75" height="75" />
   <size label="Thumbnail" width="100" height="62" />
   <size label="Small" width="112" height="69" />
   <size label="Large" width="112" height="69" />
$xml = simplexml_load_string($string);
$result = $xml->xpath("//size[@label='Large']");
// print the first (and only) member of the array
echo $result[0]->asXml();
The script would print:
<size label="Large" width="112" height="69"/>

ron @ rotflol dot cx

xpath returning references to the parent simplexml object can bite you performance wise when processing large trees.
this bit me big time so looking for a solution I came up with a simple way to speed up the code.
'dereferencing' will yield instant results, code will be lighting fast.
sacrifice is losing the reference to the parent object, for simple read-only operation this should be ok.
$data = 'data.xml';
$rs = simplexml_load_file($data);
foreach($rs->xpath('//row') as $rsrow){
// 'derefence' into a seperate xml tree for performance
$row = simplexml_load_string($rsrow->asXML());
$v = $row->xpath('//field[@name="id"]');


Xpath actually returns an array full of references to elements in the parent SimpleXML object. Therefore, in addition to preserving attributes, you can actually modify them and the result will be reflected in the parent.
$xml_str = <<<END
   <b attr="foo"/>
$xml = simplexml_load_string ($xml_str);
$result = $xml->xpath ('/a/b'); // returns an array with one element
$result[0]['attr'] = 'bar';
print $xml->asXML ();
   <b attr="bar"/>


well after testing my conclusion, if you are not interested in namespaces, is:
then <? $xmltxt->xpath("//node"); ?> will work
not getting that void array

brettz9 through yah com

Though it should according to XPath, the following does not work with this extension (nor apparently do other expressions not yielding a nodeset):
$paragraph_count = $page->xpath(\"count(//par)\");
The following will work instead, in the meantime, however:
$paragraph_count = count($page->xpath(\"//par\"));

matt@roughest d0t net

This may be obvious, but in case you're wondering, the array resulting from the xpath() operation doesn't retain any of the nodes' attributes.
In other words, it won't return any of the tags' attributes

nelson dot menezes

Regarding the note about the return of this function "not retaining tag attributes". This is not true at all (PHP 5.0.3); The attributes are kept for each member of the array returned. So this:
 <b value="1">xxx1</b>
 <b value="2">xxx2</b>
An xpath of "/a/b" would return an array with two SimpleXMLElements; each can have their tag properties read like so:


Note: xpath currently (5.0) does not work with xml documents that employ default namespaces, but a fix should be added via registerXPathNamespace() in 5.1. See for more info.


Note that this function does not ALWAYS return an array. It seems that if the specified path is not found in the document, it will return false. You need to check for the false value before using foreach, if you wish to avoid a warning (and often to handle errors or control program flow in this situation).

brettz9 through yah

I was looking to find all of a given element type with attributes which were not of a particular value (including null items).
I tried this...
...But that only worked if there was in fact some attribute.
I thought this should work (shouldn't it?), but it didn't:
  $xml->xpath("//pg[@show!='none' or !@show]");
The solution is to use the not() function:


here's a simple work-around for the lack of support for default namespaces, just overwrite the xmlns attribute of the root element:
$xml = simplexml_load_file('');
// hack to work around php 5.0's problems with default namespaces and xpath()
$xml['xmlns'] = '';
// now our queries work:
$titles = $xml->xpath('entry/title');
foreach ((array)$titles as $title) {
   print "$title\n";

Change Language

Follow Navioo On Twitter
eXTReMe Tracker