If you want to search for quoted values use the xpath concat function.
<?php
// see note "paul at pmtlogic dot com"
$xml->xpath('//zone[@country=concat("Cote d", \''\', "Ivoire")]');
?>
SimpleXMLElement::xpath
(PHP 5 >= 5.2.0)
SimpleXMLElement::xpath — Runs XPath query on XML data
Description
public array SimpleXMLElement::xpath
( string $path
)
The xpath method searches the SimpleXML node for children matching the XPath path.
Parameters
- path
-
An XPath path
Return Values
Returns an array of SimpleXMLElement objects or FALSE in case of an error.
Examples
Example #1 Xpath
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');
while(list( , $node) = each($result)) {
echo '/a/b/c: ',$node,"\n";
}
/* Relative paths also work... */
$result = $xml->xpath('b/c');
while(list( , $node) = each($result)) {
echo '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.
See Also
- SimpleXMLElement::registerXPathNamespace() - Creates a prefix/ns context for the next XPath query
- SimpleXMLElement::getDocNamespaces() - Returns namespaces declared in document
- SimpleXMLElement::getNamespaces() - Returns namespaces used in document
- Basic SimpleXML usage
jan at ingoedebanen dot nl
31-Oct-2011 08:32
yetihehe at yetihehe dot com
12-Jan-2011 11:21
Xpath actually knows which element is root regardless of element on which you execute it. Example:
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<b>
<c>code</c>
</b>
</a>
XML;
header('content-type: text/plain');
$xml = new SimpleXMLElement($string);
//relative to root
$b0=$xml->b[0]->xpath('//c');
while(list( , $node) = each($b0)) {
echo 'b[0]: //c: ',$node,"\n";
}
$b1=$xml->b[1]->xpath('//c');
while(list( , $node) = each($b1)) {
echo 'b[1]: //c: ',$node,"\n";
}
echo "\n";
//relative to current element
$b0=$xml->b[0]->xpath('.//c');
while(list( , $node) = each($b0)) {
echo 'b[0]: .//c: ',$node,"\n";
}
$b1=$xml->b[1]->xpath('.//c');
while(list( , $node) = each($b1)) {
echo 'b[1]: .//c: ',$node,"\n";
}
?>
Will return:
b[0]: //c: text
b[0]: //c: stuff
b[0]: //c: code
b[1]: //c: text
b[1]: //c: stuff
b[1]: //c: code
b[0]: .//c: text
b[0]: .//c: stuff
b[1]: .//c: code
stefan at efectos dot nl
09-Nov-2010 11:33
Having problems selecting information through xpath when there are namespaces in the XML?
Try this quick and dirty solution, but it works:
<?php
$objImageXml = new SimpleXMLElement($objImageXml->asXML());
?>
eleg
10-May-2010 01:25
to be able to use the default namespace:
<?php
$xml = simplexml_load_file('test.xfdf');
$namespaces = $xml->getDocNamespaces();
$xml->registerXPathNamespace('__empty_ns', $namespaces['']);
$result = $xml->xpath('//__empty_ns:field/@name');
?>
grummfy at gmail dot com
11-Feb-2010 01:08
On a xml that have namespace you need to do this before your xpath request (or empty array will be return) :
<?php
$string = str_replace('xmlns=', 'ns=', $string); //$string is a string that contains xml...
?>
DrupalFocus at earthlink dot net
02-Dec-2009 09:27
Looks like the relative path is relative to the SimpleXMLElement object that you call the Xpath() method on. Basically chopping off the root element.
<?php
$string = <<<XML
<foo>
<bar>
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
</bar>
</foo>
XML;
$xml = new SimpleXMLElement($string);
/* Search for children using 2nds level relative <bar><a><b><c> */
$result = $xml->xpath('a/b/c');
if ($result)
foreach($result as $node) {
drupal_set_message(t('a/b/c: @node.', array('@node' => $node)));
}
else
drupal_set_message(t('a/b/c: failed.'));
/* it relative to the SimpleXMLElement Search for 2nds level relative <bar><a><b><c> but where is foo */
$bar = $xml->bar;
$result = $bar->xpath('a/b/c');
foreach($result as $node) {
drupal_set_message(t('bar->a/b/c: @node.', array('@node' => $node)));
}
?>
results in :
#
# a/b/c: failed.
# bar->a/b/c: text.
# bar->a/b/c: stuff.
behnam@zwnj
24-Sep-2009 10:34
Seems like this function cannot return the result of XPath string() [1] function. For example if you query xpath("string(a/@b)") you will get bool(false) although the node "a" may have the attribute "b" set to a non-empty string.
[1] http://www.w3.org/TR/xpath#section-String-Functions
Anti Veeranna
17-Jun-2009 12:50
Whether it returns a false or empty array for a non-existing path seems to depend on libxml version.
I tried the following code:
<?php
$data = '<foo><bar></bar></foo>';
$xml = simplexml_load_string($data);
print gettype($xml->xpath('/first/second'));
?>
On a machine with libxml 2.6.16 and PHP 5.2.8 I got back boolean. On a different machine with libxml 2.7.3 and PHP 5.2.8/5.2.9 I got back empty array.
You can check the libxml version from command line by executing: php --ri libxml
or simply look at phpinfo() output
arealzone-czernobyl at yahoo dot de
12-Jun-2009 12:23
The simplest way to count nodes in a XML doc:
<?php
$xml = new SimpleXMLElement($xmlstr);
$xmlNode = $xml->xpath('root/yourNodeName');
$nodeCount = count($xmlNode);
echo $nodeCount;
?>
anemik
03-Jul-2008 12:46
If you want to find easly all records satisfying some condition in XML data like
....
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
</book>
...
try example below
<?php
$xmlStr = file_get_contents('data/books.xml');
$xml = new SimpleXMLElement($xmlStr);
// seach records by tag value:
// find all book records with price higher than 40$
$res = $xml->xpath("book/price[.>'40']/parent::*");
print_r($res);
?>
You will see response like:
Array (
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => bk101
)
[author] => Gambardella, Matthew
[title] => XML Developer's Guide
[genre] => Computer
[price] => 44.95
[publish_date] => 2000-10-01
[description] => An in-depth look at creating applications
with XML.
)
...
paul at pmtlogic dot com
18-Jun-2008 09:17
xpath doesn't seem to be able to handle single quotes embedded in the query itself. For instance, I want to find geo coordinates in an xml file based on country name.
xml snippet:
<zones>
<zone country="Cote d'Ivoire" fullName="Yamoussoukro" geo="6.82,-5.28" id="1050"><url prefix="1001" value="fiji.html" /><url prefix="1002" value="C" /></zone>
</zones>
The following code does not work:
<?php
$xml = simplexml_load_file("my.xml");
$result = $xml->xpath("//zone[@country='Cote d\'Ivoire']");
foreach ( $result[0]->attributes() as $key => $val ) {
print "<div class='coords'>$key: $val</div>\n";
}
?>
I have tried many variations on the embedded single quote (i.e. escape codes) but with no result. W3C offers no explanation either.
In addition, there doesn't seem to be any way of embedding wildcards in the attribute value (you can embed wildcards in the attribute name). Otherwise the following might be a reasonable substitute in this context:
<?php $result = $xml->xpath("//zone[@country='Cote d*Ivoire']"); ?>
canuemail at gmail dot com
05-May-2008 02:05
If you want to search multiple values from xml on the behalf or one value then this code can be helpfull to you.
if there is:
<Record>
<country>Pakistan</country>
<code>+92</code>
<Value>100<Value>
</Record>
then try this one:
<?php
$sxe = simplexml_load_file("countries.XML");
foreach($sxe->xpath('//RECORD') as $item) {
$row = simplexml_load_string($item->asXML());
$v = $row->xpath('//country[. ="Pakistan"]');
if($v[0]){
print $item->country;
print $item->code;
print $item->value;
}
}
?>
brettz9 through yah
13-Sep-2006 04:31
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...
<?php
$xml->xpath("//pg[@show!='none']");
?>
...But that only worked if there was in fact some attribute.
I thought this should work (shouldn't it?), but it didn't:
<?php
$xml->xpath("//pg[@show!='none' or !@show]");
?>
The solution is to use the not() function:
<?php
$xml->xpath("//pg[not(@show='none')]");
?>
ron @ rotflol dot cx
29-Mar-2006 06:07
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.
<?php
$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"]');
}
?>
drewish at katherinehouse dot com
10-Jan-2006 08:40
here's a simple work-around for the lack of support for default namespaces, just overwrite the xmlns attribute of the root element:
<?php
$xml = simplexml_load_file('http://example.com/atom_feed.xml');
// 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";
}
?>
Hugh
19-Dec-2005 12:23
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).
retardis at gmail dot com
24-Aug-2005 10:21
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.
Example:
<?php
$xml_str = <<<END
<a>
<b attr="foo"/>
</a>
END;
$xml = simplexml_load_string ($xml_str);
$result = $xml->xpath ('/a/b'); // returns an array with one element
$result[0]['attr'] = 'bar';
print $xml->asXML ();
?>
Outputs:
<a>
<b attr="bar"/>
</a>
drewish at katherinehouse dot com
11-Jul-2005 03:16
xpath() can also be used to select elements by their attributes. For a good XPath reference check out: http://www.w3schools.com/xpath/xpath_syntax.asp
<?php
$string = <<<XML
<sizes>
<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" />
</sizes>
XML;
$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"/>
matt@roughest[d0t]net
03-Sep-2004 09:17
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
