Simplexml XPath strangeness

前端 未结 1 680
刺人心
刺人心 2021-01-24 13:55

I\'m writing a script which reads and manipulates a KML (xml) document. Below is a snippet of the document I\'m reading:



        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 14:17

    load('file.xml');    // returns true
    $xp = new DOMXPath($doc);
    $xp->registerNamespace('ge', 'http://earth.google.com/kml/2.2');
    
    $places = $xp->query("//ge:Placemark");
    echo $places->length;         // --> 0 ??!!??
    $everything = $xp->query("//*"); // (so I know that the XPath isn't fully borked)
    
    //echo $doc->saveXML();
    

    Apparently you have to register the 'ge' namespace and query it as such, at least this is what I came up with after a few minutes googling. I guess sometimes we forget we're dealing with namespaces :p

    0 讨论(0)
提交回复
热议问题