문제

I must be missing something here, but I am unable to figure it out.

I am using SimpleXML to parse a RSS and ATOM feed, but the problem is, using the same xpath method, I am not able to select <link> from ATOM feed while it's working with RSS feed.

Samples from both feed

<?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&#039;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>
...

it seems $xml->channel->xpath('atom:link[@rel="self"]'); works with RSS feed, but $xml->xpath('link[@rel="self"]'); doesn't work with the ATOM feed. I suspect I am messing up namespace in the second query, but didn't find the right xpath query.

Anyone had an idea?

도움이 되었습니까?

해결책 2

Since XPath 1.0 spec doesn't support default namespace (and since PHP SimpleXML use libxml, which as far as I know, implements 1.0 spec), we need to register the default namespace first for xpath to work properly.

So $xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom') first, then we can do $xml->xpath('atom:link[@rel="self"]'); successfully.

다른 팁

In the second case, a default xmlns is set at the feed root element, viz

<feed xmlns="http://www.w3.org/2005/Atom"  ...

Means that feed, and nested elements are now in http://www.w3.org/2005/Atom. You will need to adjust your xpath to accomodate this.

The xpath would be

atom:feed/atom:link[@rel="self"]

Where xmlns:atom = 'http://www.w3.org/2005/Atom'

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top