0
votes

Getting XML from this URL:

$xml = simplexml_load_file('http://geocode-maps.yandex.ru/1.x/?geocode=37.71677,55.75208&kind=metro&spn=1,1&rspn=1');

print_r($xml) shows that XML loaded, but xpath always returns empty array. I tried:

$xml->xpath('/');
$xml->xpath('/ymaps');
$xml->xpath('/GeoObjectCollection');
$xml->xpath('/ymaps/GeoObjectCollection');
$xml->xpath('//GeoObjectCollection');
$xml->xpath('precision');

Why I got empty array? Hope I just missing something easy.

1

1 Answers

2
votes

It might be rather easy, but I guess it is also the most common mistake in the history of XML: You are forgetting namespaces!

A lot of elements in the given XML are changing the default namespace and you have to consider that in your XPath.

You can first register your namespace like so:

$xml->registerXPathNamespace('y', 'http://maps.yandex.ru/ymaps/1.x');
$xml->registerXPathNamespace('a', 'http://maps.yandex.ru/attribution/1.x');

and then you can query your data:

$xml->xpath('//y:ymaps/y:GeoObjectCollection');