问题
I can query the description
nodes in an RSS feed and return a result like this:
$xpath = new DOMXPath($xmlDoc);
$items = $xpath->query('/rss/channel/item/description/..');
foreach($items as $number => $item){}
But it returns nothing when I query the entry
nodes from an Atom feed like this (in which I follow the same pattern as RSS's above):
$xpath = new DOMXPath($xmlDoc);
$items = $xpath->query('/feed/entry/..');
foreach($items as $number => $item){}
What am I missing?
回答1:
There is no RSS namespace, whereas Atom elements are in the following namespace:
http://www.w3.org/2005/Atom
You can see this by looking at your document's feed
element, which probably looks something like this:
<feed xmlns="http://www.w3.org/2005/Atom">
You need to register this namespace before querying for elements in it:
$xpath->registerNamespace('a', 'http://www.w3.org/2005/Atom');
And then use the chosen namespace prefix in your expression:
$items = $xpath->query('/a:feed/a:entry');
来源:https://stackoverflow.com/questions/8068997/how-to-query-the-entry-nodes-from-an-atom-feed