我无法使用SimpleXML和XPATH解析此XML文件。
<feed xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom" xmlns:pamedia="http://paimages.co.uk/pamedia.htm">
<title>
Image / video search results
</title>
<subtitle>
Images / video found containing the search string provided
</subtitle>
<pamedia:found>
47
</pamedia:found>
<pamedia:perpage>
100
</pamedia:perpage>
<pamedia:page>
1
</pamedia:page>
<opensearch:totalResults>
47
</opensearch:totalResults>
<opensearch:itemsPerpage>
100
</opensearch:itemsPerpage>
<opensearch:startIndex>
1
</opensearch:startIndex>
<id>
http://images.pressassociation.com/cgi/search_api/?state=search&q=test+cricket+-pakistan+allincaption:+fast+ball
</id>
<link rel="self" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=test+cricket+-pakistan+allincaption:+fast+ball"></link>
<updated>
2013-11-19T09:46:42Z
</updated>
<link rel="self" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=test+cricket+-pakistan+allincaption:+fast+ball">
<updated>
2013-11-19T09:46:42Z
</updated>
<name>
Press Association Images
</name>
<email>
REDACTED
</email>
</link>
<entry>
<pamedia:media-type>
image/jpeg
</pamedia:media-type>
<pamedia:event_date>
2011-06-21
</pamedia:event_date>
<pamedia:urn>
11019393
</pamedia:urn>
<pamedia:domain>
2
</pamedia:domain>
<pamedia:domain_prefix>
PA
</pamedia:domain_prefix>
<link type="application/vnd.iptc.g2.newsitem+xml" href="http://images.pressassociation.com/meta/2.11019393.xml"></link>
<link rel="related" href="http://images.pressassociation.com/meta/2.11019393.html" type="text/html"></link>
<link rel="related" href="http://images.pressassociation.com/empicsthumbnail/vol111/block2204/11019393.jpg" type="image/jpeg"></link>
<media:thumbnail width="153" medium="image" height="127" url="http://images.pressassociation.com/empicsthumbnail/vol111/block2204/11019393.jpg" type="image/jpeg"></media:thumbnail>
<media:content expression="sample" medium="image" width="616" height="511" url="http://images.pressassociation.com/image/preview/2.11019393.jpg" type="image/jpeg"></media:content>
<media:copyright>
Associated Press
</media:copyright>
<media:content expression="full" medium="photo" width="1657" height="2000" url="http://images.pressassociation.com/image/2.11019393.jpg" type="image/jpeg"></media:content>
<updated>
2011-06-21T22:48:19Z
</updated>
<summary type="html">
West Indies' fast bowler Fidel Edwards, left, reacts after his wicket keeper Carlton Baugh, unseen, couldn't hold his delivery as India's batsman Virat Kohli, right, watches the ball reach the boundary in the second innings on the second day of their first cricket Test match in Kingston, Jamaica, Tuesday June 21, 2011. (AP Photo/Andres Leighton)
</summary>
<rights type="html">
UK picture buyers only JAM163
</rights>
<id>
http://images.pressassociation.com/meta/2.11019393.xml
</id>
<title type="html">
Jamaica India West Indies Cricket
</title>
<category term="S"></category>
<author>
<name>
Andres Leighton/AP
</name>
</author>
</entry>
<entry>
<pamedia:media-type>
image/jpeg
</pamedia:media-type>
<pamedia:event_date>
2011-06-21
</pamedia:event_date>
<pamedia:urn>
11019370
</pamedia:urn>
<pamedia:domain>
2
</pamedia:domain>
<pamedia:domain_prefix>
PA
</pamedia:domain_prefix>
<link type="application/vnd.iptc.g2.newsitem+xml" href="http://images.pressassociation.com/meta/2.11019370.xml"></link>
<link rel="related" href="http://images.pressassociation.com/meta/2.11019370.html" type="text/html"></link>
<link rel="related" href="http://images.pressassociation.com/empicsthumbnail/vol111/block2204/11019370.jpg" type="image/jpeg"></link>
<media:thumbnail width="161" medium="image" height="127" url="http://images.pressassociation.com/empicsthumbnail/vol111/block2204/11019370.jpg" type="image/jpeg"></media:thumbnail>
<media:content expression="sample" medium="image" width="650" height="511" url="http://images.pressassociation.com/image/preview/2.11019370.jpg" type="image/jpeg"></media:content>
<media:copyright>
Associated Press
</media:copyright>
<media:content expression="full" medium="photo" width="1571" height="2000" url="http://images.pressassociation.com/image/2.11019370.jpg" type="image/jpeg"></media:content>
<updated>
2011-06-21T22:35:22Z
</updated>
<summary type="html">
India's batsman Virat Kohli ducks to avoid being hit by a short ball off West Indies' fast bowler Fidel Edwards in the second innings on the second day of their first cricket Test match in Kingston, Jamaica, Tuesday June 21, 2011. (AP Photo/Andres Leighton)
</summary>
<rights type="html">
UK picture buyers only JAM160
</rights>
<id>
http://images.pressassociation.com/meta/2.11019370.xml
</id>
<title type="html">
Jamaica India West Indies Cricket
</title>
<category term="S"></category>
<author>
<name>
Andres Leighton/AP
</name>
</author>
</entry>
我正在尝试选择标签并撤回其中包含的信息,特别是链接[3]块,这样我就可以将链接嵌入缩略图中,我已经连接到API并将数据提取回来但是我的我尝试过的XPath查询一定是错的。
我试过$ query = / entry / link [3] / href和其他一些无济于事。我是使用XPath查询XML数据的新手。任何帮助将不胜感激。
答案 0 :(得分:2)
实际上存在多种错误:
首先,正如Michael Kay已经指出的那样,所讨论的XML节点具有命名空间,并且查询查找没有命名空间的节点。您必须使用前缀为XPath查询注册名称空间(您可以任意选择,我将在我的示例中使用'namespace'),然后使用它来选择节点。
其次,查询缺少'/ feed'第一段。
第三,选择href 属性需要... / @ href,因为... / href会选择一个子节点。
完整代码(在PHP语法中,不知道SimpleXML是否可用于其他语言):
$xml = new SimpleXMLElement('... xml source text ...');
$xml->registerXPathNamespace('namespace', 'http://www.w3.org/2005/Atom');
$query = '/namespace:feed/namespace:entry/namespace:link[3]/@href';
debug($xml->xpath($query));
die();
请注意,由于某种原因,这将返回仅包含href属性的元素对象,而人们希望它返回属性节点。但我认为这更多是由于SimpleXML的奇怪API而不是XPath本身。