Q:

Cannot retrieve data from XML using xpath() function in PHP

I want to parse this xml file and use XPATH retrieve some fields (this is how I see xml file by the way, you can download it from here):
I try the following code to get the edition of the book:
$file_copac = "http://copac.ac.uk/search?isn=$isbn&rn=1&format=XML+-+MODS";
$xml = simplexml_load_file($file_copac) or die("cannot_get_data");
$temp = $xml->xpath('//edition');
var_dump($temp);

I also tried 'edition' but the result is empty array for both:
array(0) { }

I tried full path using '/mods/originInfo[1]/edition' which ended with an XPATH error. I solve problem with this notation:
$edition = (string)$xml->mods->originInfo[1]->edition;

However I wonder the problem with xpath.

A:

Looks like problem with default (empty) namespace, workaround for this:
$namespaces = $xml->getDocNamespaces(); 
$xml->registerXPathNamespace('__DEFAULT_NS__', $namespaces['']);
$r = $xml->xpath('//__DEFAULT_NS__:edition');

var_dump((string)$r[0]); //string(8) "8th. ed."

