EnexoOnoma
EnexoOnoma

Reputation: 8836

Simple xpath question that drives me crazy

below is the structure of a feed I managed to print the content using this xpath
$xml->xpath('/rss/channel//item')

the structure

<rss><channel><item><pubDate></pubDate><title></title><description></description><link></link><author></author></item></channel></rss>

However some of my files follow this structure

<feed xmlns="http://www.w3.org/2005/Atom" .....><entry><published></published><title></title><description></description><link></link><author></author></entry></feed>

and I guessed that this should be the xpath to get the content of entry

$xml->xpath('/feed//entry')

something that proved me wrong.

My question is what is the right xpath to use? Am i missing something else ?

This is the code

<?php

$feeds = array('http://feeds.feedburner.com/blogspot/wSuKU');


$entries = array();
foreach ($feeds as $feed) {
    $xml = simplexml_load_file($feed);
    $entries = array_merge($entries, $xml->xpath('/feed//entry'));
}

echo "<pre>"; print_r($entries); echo"</pre>";

?>

Upvotes: 2

Views: 1073

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66714

If you want a single XPath expression that will work when applied to either an RSS or an ATOM feed, you could use either of the following XPath expressions:

This one is the most precise, but also the most verbose:

(/rss/channel/item 
  | /*[local-name()='feed' and namespace-uri()='http://www.w3.org/2005/Atom']
      /*[local-name()='entry' and namespace-uri()='http://www.w3.org/2005/Atom'])

This one ignores the namespace of the ATOM elements and just matches on their local-name():

(/rss/channel/item | /*[local-name()='feed']/*[local-name()='entry'])

This one is the most simple, but the least precise and the least efficient:

/*//*[local-name()='item' or local-name()='entry']

Upvotes: 2

unbeli
unbeli

Reputation: 30228

try this:

$xml->registerXPathNamespace('f', 'http://www.w3.org/2005/Atom');
$xml->xpath('/f:feed/f:entry');

Upvotes: 4

Related Questions