Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7626301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:18:26+00:00 2026-05-31T05:18:26+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:18:27+00:00Added an answer on May 31, 2026 at 5:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when accessing foo() of base using derived class's object. #include <iostream> class base {
I am trying to use the youtube API to pulldown some videos for my
I'm accessing the address book and have this line and am compiling using ARC:
Occasionally when accessing OpenX 2.8.1 via its XMLRPC api (using the ruby 'openx' gem),
I'm accessing the feed of a user's favorite videos through the YouTube API. How
When accessing a MySQL database on low level using python, I use the MySQLdb
In accessing PDH.dll (a Win32 API) (see link) using P/Invoke , it turns out
When accessing a member of some class, I can use e.g.: this->myVar = 10
When accessing an object in a DataTable retrieved from a database, are there any
While accessing a webservice which is availbale at http://recpushdata.cyndigo.com/jobs.asmx , I am getting this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.