2012-03-12 10 views
5

मैं YouTube पर gData Api तक पहुंच रहा हूं। मैं संदर्भ के लिए this xml का उपयोग करूंगा।SimpleXMLElement, xpath, और उप तत्व

मैं एक बच्चे 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%[email protected]=%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" 
    } 
} 

मैं:

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 अभी भी मूल तत्व क्यों खोज रहा है? और सबसे महत्वपूर्ण बात यह है कि मैं केवल बाल तत्व कैसे खोज सकता हूं?

उत्तर

5

आपको अपनी अभिव्यक्ति से // को बैक अप के रूप में हटा देना है और फिर पूरे दस्तावेज़ में अभिव्यक्ति लागू करना है। जो आप खोज रहे हैं वह एक स्लैश / है, जो दिए गए दस्तावेज़ खंड की जड़ से शुरू होता है।

यह चाल चलाना चाहिए। :)

संपादित करें: पूरी तरह से स्लैश को छोड़ने से चाल चलनी चाहिए।

+1

बस "स्लैश को पूरी तरह छोड़ने से चाल चलनी चाहिए।" मेरे मामले में काम किया है। – MingalevME

संबंधित मुद्दे