Jump to content

simplexml xpath not working


ngreenwood6

Recommended Posts

I am trying to parse a blogspot feed using xpath but it doesnt seem to be working with anything that I try. I am not sure if it is because of the namespaces or what but I was hoping someone could help me. Here is the code:

 

        $xml = simplexml_load_file('http://feeds.feedburner.com/blogspot/MKuf');

   

    $next = $xml->xpath("//link[@rel=next]");

    print_r($next);

 

This is just returning an empty array and it should not be. I tried it doing just link or just entry and it still is returning empty. The only one I can run on it that works is *. Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/231014-simplexml-xpath-not-working/
Share on other sites

Since the XML document has a default namespace (for Atom), you need to tell XPath that it exists and in your query you need to ask for link elements within that default namespace. Since you're looking for only the next link of the feed (I'm guessing you don't want any from the entries) then there's also no need for // in the XPath query.

 

$xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
$next = $xml->xpath("atom:link[@rel='next']");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.