1

I've seen various threads on this matter but none have been able to help me. Basically, I'm interpreting a KML file with the intent of loading it in to a map but the namespaces are really screwing me up. Here's an example of the KML:

<?xml version="1.0" encoding="UTF-8">
<kml xmlns="http://www.opengis.net/kml/2.2" 
     xmlns:gx="http://www.google.com/kml/ext/2.2" 
     xmlns:kml="http://www.opengis.net/kml/2.2" 
     xmlns:atom="http://www.w3.org/2005/Atom">
 <Folder>
  <name>leeds primary schools (1 - 10)</name>
  <open>1<open>
  <Placemark>
      blah blah blah
  </Placemark>
  [etc etc etc]

So the idea is that I want to get all the Placemark elements on the page. So I'm using this code:

$xml = simplexml_load_string($xml_string);
$xml->registerXPathNamespace("n", "http://www.opengis.net/kml/2.2");
$Placemarks = $xml->xpath("/n:Placemark");

And yet, I'm getting diddly-squit out, just an empty array. Can anyone see anything I'm doing wrong?

Community
  • 1
  • 1
Jack Bliss
  • 114
  • 6
  • I have always come around this by using `$xml->children()` - I've never bothered to actually understand how namespaces are supposed to work correctly with PHP.. – hank Feb 03 '13 at 16:43
  • Please make visible which of the various threads you've tried and what did not work specifically in your case? Please post all the codes of all your tries. Additionally I have to add: Why do you think should your code work? Even without taking namespaces into account it would not work, there are no such elements you ask for in that axis. – hakre Feb 06 '13 at 11:21

1 Answers1

1

$Placemarks = $xml->xpath("/n:Placemark");

This tries to select the top element named "n:Placemark".

However, the top element isn't a "Placemark" -- it is a "klm"

Solution:

You want:

$Placemarks = $xml->xpath("/*/n:Folder/n:Placemark");
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431