php-无法选择带有xpath的元素
作者:互联网
我必须在这里丢失一些东西,但是我无法弄清楚.
我正在使用SimpleXML来解析RSS和ATOM提要,但是问题是,使用相同的xpath方法,我无法选择< link>从ATOM提要与RSS提要一起使用时.
两种饲料的样品
<?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 feed,但$xml-> xpath(‘link [@ rel =“ self”]’);不适用于ATOM供稿.我怀疑我在第二个查询中弄乱了名称空间,但没有找到正确的xpath查询.
有人有主意吗?
解决方法:
在第二种情况下,在提要根元素viz上设置了默认的xmlns.
<feed xmlns="http://www.w3.org/2005/Atom" ...
意味着feed和嵌套元素现在位于http://www.w3.org/2005/Atom中.您将需要调整xpath来适应这一点.
xpath将是
atom:feed/atom:link[@rel="self"]
其中xmlns:atom =’http://www.w3.org/2005/Atom’
标签:atom-feed,rss,xpath,xml,php 来源: https://codeday.me/bug/20191121/2054504.html