【发布时间】:2023-03-26 10:27:01
【问题描述】:
我无法使用 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 数据非常陌生。任何帮助将不胜感激。
【问题讨论】:
-
搜索“XPath 默认命名空间”,平均每天有一个 SO 问题来自那些没有弄清楚当元素在命名空间中时,在 XPath 中搜索不合格名称的人找不到他们。