如何查询<entry>来自 ATOM 提要的节点 [英] How to query the <entry> nodes from an ATOM feed
问题描述
我可以查询 RSS 提要中的 description
节点并返回如下结果:
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){}
但是当我从这样的 Atom 提要中查询 entry
节点时,它没有返回任何内容(我遵循与上述 RSS 相同的模式):
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){}
我错过了什么?
推荐答案
没有 RSS 命名空间,而 Atom 元素在以下命名空间中:
There is no RSS namespace, whereas Atom elements are in the following namespace:
http://www.w3.org/2005/Atom
您可以通过查看文档的 feed
元素来看到这一点,它可能如下所示:
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');
这篇关于如何查询<entry>来自 ATOM 提要的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!