我一定是在这里遗漏了什么,但我无法弄明白。
我正在使用 SimpleXML 解析 RSS 和 ATOM 提要,但问题是,使用相同的 xpath 方法,我无法选择 <link>
在使用 RSS 提要时从 ATOM 提要中获取。
来自两个提要的样本
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
<title>比特客栈的文艺复兴</title>
<atom:link href="http://bitinn.net/feed/" rel="self" type="application/rss+xml" />
<link>http://bitinn.net</link>
<description>We don't choose who we are, but we do choose who we become.</description>
<lastBuildDate>Sun, 27 Apr 2014 14:43:22 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
...
<?xml version="1.0" encoding="UTF-8"?>
<feed
xmlns="http://www.w3.org/2005/Atom"
xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en-US" xml:base="http://bitinn.net/wp-atom.php">
<title type="text">比特客栈的文艺复兴</title>
<subtitle type="text">We don't choose who we are, but we do choose who we become.</subtitle>
<updated>2014-04-27T14:43:22Z</updated>
<link rel="alternate" type="text/html" href="http://bitinn.net"/>
<id>http://bitinn.net/feed/atom/</id>
<link rel="self" type="application/atom+xml" href="http://bitinn.net/feed/atom/"/>
<generator uri="http://wordpress.org/" version="3.9">WordPress</generator>
<link rel="hub" href="http://pubsubhubbub.appspot.com"/>
<link rel="hub" href="http://superfeedr.com/hubbub"/>
<entry>
<author>
<name>店长</name>
<uri>http://bitinn.net/</uri>
</author>
...
好像$xml->channel->xpath('atom:link[@rel="self"]');
适用于 RSS 提要,但 $xml->xpath('link[@rel="self"]');
不适用于 ATOM 提要。我怀疑我在第二个查询中弄乱了命名空间,但没有找到正确的 xpath 查询。
有人有想法吗?
最佳答案
在第二种情况下,默认的 xmlns
设置在 feed
根元素,即
<feed xmlns="http://www.w3.org/2005/Atom" ...
意味着提要和嵌套元素现在位于 http://www.w3.org/2005/Atom
中。您将需要调整您的 xpath 以适应这种情况。
xpath 是
atom:feed/atom:link[@rel="self"]
哪里 xmlns:atom = 'http://www.w3.org/2005/Atom'
关于php - 无法使用 xpath 选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363121/