note 54586 deleted from function.xpath-eval by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:42:28 +0000 |
| Subject: | note 54586 deleted from function.xpath-eval by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207426@lists.php.net to get a copy of this message | ||
Note Submitter: marius kreis (email to mariuskreis . de)
----
If the namespace is subject to change you can write even more portable code if you extend brandon
dot whitehead at orst dot edu's solution like this:
$doc = domxml_open_mem($xml);
$xpath = $doc->xpath_new_context();
$namespace = $xpath->xpath_eval('namespace-uri(//*)')->value; // returns the
namespace uri
xpath_register_ns($xpath, "pre", $namespace); // sets the prefix "pre" for the
namespace
$obj = $xpath->xpath_eval('//pre:Offer'); // finds all Offer tags
$nodeset = $obj->nodeset;
print_r($nodeset);
This code will determine the namespace of the root element and set a prefix for XPath queries. Thus
it doesn't matter if the namespace is changing in your XML (like some webservices do...)