Wednesday 20 June 2012

XPath expressions and namespaces

Haven't touched this blog in a long long time! Time for a quick snippet that is not symfony related, but might be quite useful for some, specially if you use XML data on a frequent basis.
PHP offers us XPath expressions when handling XML data through SimpleXML. This allows us to query for data in a great and quick way, but one thing baffled me when using it for the first time: it didn't work when querying for data with no namespace (default namespace). After quite a while looking for a solution on the web, it seems XPath needs the empty namespace to be registered. For example:

$xml = '
<?xml version="1.0" encoding="UTF-8"?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
 </movie>
</movies>
'

$movies = new SimpleXMLElement($xml);
$namespaces = $movies->getDocNamespaces(true);
$movies->registerXPathNamespace('__empty_ns', $namespaces['']);

Now you can query using the __empty_ns namespace (or whatever is your favourite flavour in  empty namespacing)!

$phpMovie = $movies->xpath('//__empty_ns:movie/title[.="PHP: Behind the Parser"]');


Pages