【问题标题】:Trouble parsing the XML file with XPath and SimpleXML使用 XPath 和 SimpleXML 解析 XML 文件时遇到问题
【发布时间】: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 中搜索不合格名称的人找不到他们。

标签: php xpath simplexml


【解决方案1】:

实际上有多个错误:

首先,正如 Michael Kay 已经指出的那样,所讨论的 XML 节点具有命名空间,而查询查找没有命名空间的节点。您必须使用前缀为 XPath 查询注册命名空间(您可以任意选择,我将在示例中使用“命名空间”),然后使用它来选择节点。

其次,查询缺少“/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 属性的 element 对象,而人们会期望它返回 attribute 节点。但我认为这更多是由于 SimpleXML 奇怪的 API 而不是 XPath 本身。

【讨论】:

  • 这行得通,谢谢你用简洁的方式解释它。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 2012-06-02
  • 2023-03-18
  • 2012-02-06
  • 2015-05-11
  • 1970-01-01
  • 2013-09-17
相关资源
最近更新 更多