dox19458 2012-03-12 03:41
浏览 32
已采纳

SimpleXMLElement,xpath和子元素

I am accessing the gData Api on YouTube. I'll use this xml for reference.

I'm using xpath on a child SimpleXMLElement object, but rather than the xpath searching ONLY the child element and its children, it seems to still be searching from the root down.

I have the following code:

<?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"));
}

Rather than the expected:

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"
...

So, even though I'm work on a child element of the root element, why is xpath still searching the parent element? And more importantly, how can I search just the child element?

  • 写回答

1条回答 默认 最新

  • douyi3632 2012-03-12 03:54
    关注

    You have to remove the // from your expression as it backs up and then applies the expression to the entire document. What you're looking for is a single slash /, which starts from the root of the given document fragment.

    That should do the trick. :)

    edit: completely omitting the slash should do the trick as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示