ngreenwood6 Posted March 18, 2011 Share Posted March 18, 2011 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. Quote Link to comment Share on other sites More sharing options...
salathe Posted March 18, 2011 Share Posted March 18, 2011 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']"); Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2011 Author Share Posted March 18, 2011 Thanks for the reply. Is there a way to dynamically know the namespaces and register them? Quote Link to comment Share on other sites More sharing options...
salathe Posted March 21, 2011 Share Posted March 21, 2011 Sure, each SimpleXMLElement object has two methods for retrieving namespaces: getDocNamepaces and getNamespaces. You can loop over the return value from whichever of those you like most, registering them one at a time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.