PHP xpath: extract all nodes, even with namespaces

namespacesPHPxml

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="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <id>tag:youtube.com,2008:standardfeed:global:most_popular</id>
  <entry>
     <id>tag:youtube.com,2008:video:_OBlgSz8sSM</id>
     <author>
        <name>HDCYT</name>
        <uri>http://gdata.youtube.com/feeds/api/users/HDCYT</uri>
        <yt:userId>fHZCZHykS3IQDPyFfnqjWg</yt:userId>
      </author>
  </entry>
</feed>

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', 'http://www.w3.org/2005/Atom');
$xml->registerXPathNamespace('yt', 'http://gdata.youtube.com/schemas/2007');
$entries = $xml->xpath("//a:entry");
foreach($entries as $t) {
    print_r($t);
}

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

SimpleXMLElement Object
(
    [id] => tag:youtube.com,2008:video:_OBlgSz8sSM
    [author] => SimpleXMLElement Object
    (
        [name] => HDCYT
        [uri] => http://gdata.youtube.com/feeds/api/users/HDCYT
    )
 )

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

Thanks

Best Answer

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