Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to PHP's simplexml and xpath implementation, but here's what I want to do:

I have this XML file (excerpt from a youtube API response):

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="" xmlns:yt="">

What I want to do is iterate over each "entry" tag (there are more but only one is listed) and extract various values (like author -> yt:userId) for each entry.

My code looks like this:

$xml = simplexml_load_string($xmlString);
$xml->registerXPathNamespace('a', '');
$xml->registerXPathNamespace('yt', '');
$entries = $xml->xpath("//a:entry");
foreach($entries as $t) {

The problem is, the extracted object does not contain anything outside its namespace (or the default namespace):

SimpleXMLElement Object
    [id] =>,2008:video:_OBlgSz8sSM
    [author] => SimpleXMLElement Object
        [name] => HDCYT
        [uri] =>

So... how do I keep the yt:userId tag in my xpath result?


share|improve this question
up vote 0 down vote accepted

It seems I have been mislead by the print_r function. Even if content in other namespaces does not show up with print_r, it's still there.

To access the data I needed, I had to do the following on the $t object:

 $userid = $t->xpath("//yt:userId");
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.