Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用XPath和SimpleXML解析XML文件时出现问题_Php_Xpath_Simplexml - Fatal编程技术网

Php 使用XPath和SimpleXML解析XML文件时出现问题

Php 使用XPath和SimpleXML解析XML文件时出现问题,php,xpath,simplexml,Php,Xpath,Simplexml,使用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> Im

使用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>

图像/视频搜索结果
找到包含所提供搜索字符串的图像/视频
47
100
1.
47
100
1.
http://images.pressassociation.com/cgi/search_api/?state=search&q=test+板球+-巴基斯坦+联盟选项:+快速+球
2013-11-19T09:46:42Z
2013-11-19T09:46:42Z
新闻协会图像
编辑
图像/jpeg
2011-06-21
11019393
2.
帕
美联社
2011-06-21T22:48:19Z
2011年6月21日,星期二,牙买加金斯敦,西印度群岛的快投球手菲德尔·爱德华兹(左)在其第一场板球测试赛的第二天,在印度的击球手维拉特·科利(右)看着球在第二局到达边界时,他的边线守门员卡尔顿·鲍夫(Carlton Baugh)在看不见的情况下做出反应。(美联社照片/安德烈斯莱顿)
仅限英国图片买家
http://images.pressassociation.com/meta/2.11019393.xml
牙买加印度西印度群岛板球
安德烈斯莱顿/美联社
图像/jpeg
2011-06-21
11019370
2.
帕
美联社
2011-06-21T22:35:22Z
2011年6月21日,星期二,在牙买加金斯敦举行的第一场板球测试赛的第二天,印度击球手维拉特·科利在第二局躲避西印度群岛快速投球手菲德尔·爱德华兹的短球。(美联社照片/安德烈斯莱顿)
仅限英国图片买家
http://images.pressassociation.com/meta/2.11019370.xml
牙买加印度西印度群岛板球
安德烈斯莱顿/美联社
我正在尝试选择标记并提取其中包含的信息,特别是链接[3]块,这样我就可以将链接嵌入到缩略图中,因为我已经连接到API并提取了数据,但我尝试的XPath查询肯定是错误的


我尝试了$query=/entry/link[3]/href和其他一些方法,但都没有成功。我对使用XPath查询XML数据非常陌生。任何帮助都将不胜感激。

事实上有很多错误:

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

其次,查询缺少“/feed”第一段

第三,选择href属性需要…/@href,因为…/href将选择一个子节点

完整代码(在PHP语法中,不知道SimpleXML是否在其他语言中可用):


请注意,出于某种原因,这将返回一个仅包含href属性的元素对象,而人们希望它返回属性节点。但我认为这更多是由于SimpleXML奇怪的API,而不是XPath本身

搜索“XPath默认名称空间”,人们平均每天会问一个这样的问题:当元素位于名称空间中时,在XPath中搜索不合格名称将找不到它们。这很有效,感谢您以简洁的方式解释。非常感谢。
$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();