11

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.

ngreenwood6
  • 8,108
  • 11
  • 33
  • 52
  • Your xpath seems to be wrong it actually returns zero elements. What do you try to accomplish? – Alex Bailey Mar 18 '11 at 15:43
  • The document has a default namespace which you have to register before you can query it with XPath. – Gordon Mar 18 '11 at 15:54
  • 1
    possible duplicate of [DOM XPath Query Help](http://stackoverflow.com/questions/5341995/dom-xpath-query-help) – Gordon Mar 18 '11 at 15:55
  • Thats what I was thinking gordon but I tried that as well and it did not seem like it was working either, but maybe I didnt do it right (an example would be nice). I am trying to select the root link element with the rel attribute of next. – ngreenwood6 Mar 18 '11 at 15:59

2 Answers2

13

Like already said in the comment to you question, the document has a default namespace which you have to register before you can query it with XPath.

Since the linked duplicate only shows how to do it with DOM, I'll add an SimpleXml example

$feed = simplexml_load_file('http://feeds.feedburner.com/blogspot/MKuf');
$feed->registerXPathNamespace('f', 'http://www.w3.org/2005/Atom');
foreach ($feed->xpath('//f:link[@rel="next"]') as $link) {
    var_dump($link);
}

Manual Page: http://de.php.net/manual/de/simplexmlelement.registerxpathnamespace.php

Live Demo

Gordon
  • 312,688
  • 75
  • 539
  • 559
4

As Gordon said, you need to register the XML document's default namespace, like this:

$xml->registerXPathNamespace('default', 'http://www.w3.org/2005/Atom');

And then use the default prefix to refer to normal elements:

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

Newer versions may allow you to define the default ('') namespace. See http://www.qc4blog.com/?p=281 for more information.

Philip Hanson
  • 1,847
  • 2
  • 15
  • 24
  • thanks i actually discovered that as well. I tried doing it with the '' one before and thats why it was failing. – ngreenwood6 Mar 18 '11 at 16:20
  • Did one of these answers solve your problem? Let us know by clicking the check mark next to it and mark one as an answer. – Philip Hanson Apr 11 '11 at 20:15