SimpleXMLElement、xpath和子元素

5

我正在访问YouTube上的gData API。我将使用此xml作为参考。

我在子SimpleXMLElement对象上使用xpath,但是xpath似乎不仅搜索子元素及其子元素,而且还从根目录向下搜索。

我有以下代码:

<?php

date_default_timezone_set('Australia/Sydney');
$url = "http://gdata.youtube.com/feeds/api/playlists/58FD3A7244B64B99?prettyprint=true&alt=atom&v2=1&fields=title,subtitle,logo,entry%28link%5B@rel=%27alternate%27%5D,id,title,content,author,yt:statistics%29";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
$rawResponse = curl_exec($curl);

$xmlData = simplexml_load_string($rawResponse);
$xmlData->registerXPathNamespace('yt', 'http://gdata.youtube.com/schemas/2007');

foreach($xmlData->entry as $entry) {
    var_dump($entry->asXml());
    myFunction($entry); die();
}

function myFunction(SimpleXMLElement $xml)
{
    var_dump($xml->xpath("//yt:statistics"));
}

不是预期的那样:

string(666) "<entry>
                <id>http://gdata.youtube.com/feeds/api/playlists/58FD3A7244B64B99/PLlwIr0olq0UxVV_ouqclCE0xRZvs2Lytl</id>
                <title type="text">Zero Punctuation on The Escapist</title>
                <content type="text">Zero Punctuation picks apart the games so you don't have to. View new episodes every Wednesday only
at http://www.escapistmagazine.com</content>
                <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=7EpzwuZOvKY&amp;feature=youtube_gdata"/>
                <author>
                        <name>theescapistmagazine</name>
                        <uri>http://gdata.youtube.com/feeds/api/users/theescapistmagazine</uri>
                </author>
                <yt:statistics favoriteCount="256" viewCount="188598"/>
        </entry>"
object(SimpleXMLElement)#5 (1) {
  ["@attributes"]=>
  array(2) {
    ["favoriteCount"]=>
    string(3) "256"
    ["viewCount"]=>
    string(6) "188598"
  }
}

I get:

string(666) "<entry>
                <id>http://gdata.youtube.com/feeds/api/playlists/58FD3A7244B64B99/PLlwIr0olq0UxVV_ouqclCE0xRZvs2Lytl</id>
                <title type="text">Zero Punctuation on The Escapist</title>
                <content type="text">Zero Punctuation picks apart the games so you don't have to. View new episodes every Wednesday only
at http://www.escapistmagazine.com</content>
                <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=7EpzwuZOvKY&amp;feature=youtube_gdata"/>
                <author>
                        <name>theescapistmagazine</name>
                        <uri>http://gdata.youtube.com/feeds/api/users/theescapistmagazine</uri>
                </author>
                <yt:statistics favoriteCount="256" viewCount="188598"/>
        </entry>"
array(25) {
  [0]=>
  object(SimpleXMLElement)#5 (1) {
    ["@attributes"]=>
    array(2) {
      ["favoriteCount"]=>
      string(3) "256"
      ["viewCount"]=>
      string(6) "188598"
    }
  }
  [1]=>
  object(SimpleXMLElement)#6 (1) {
    ["@attributes"]=>
    array(2) {
      ["favoriteCount"]=>
      string(4) "4787"
      ["viewCount"]=>
      string(7) "1276435"
    }
  }
  [2]=>
  object(SimpleXMLElement)#7 (1) {
    ["@attributes"]=>
    array(2) {
      ["favoriteCount"]=>
      string(4) "7628"
      ["viewCount"]=>
      string(7) "1702845"
...

那么,即使我在根元素的子元素上工作,为什么xpath仍然搜索父元素?更重要的是,我如何只搜索子元素?

1个回答

5
您需要将表达式中的//移除,因为它会备份然后将表达式应用于整个文档。您要寻找的是单斜杠/,它从给定文档片段的根开始。这样应该就可以了 :)
编辑:完全省略斜杠也可以解决问题。

1
在我的情况下,“完全省略斜杠也可以解决问题。” - MingalevME

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接