Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.