1

xml:

<entry>
  <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://picasaweb.google.com/data/feed/api/user/xy/albumid/531885671007533108" /> 
  <link rel="alternate" type="text/html" href="http://picasaweb.google.com/xy/Cooking" /> 
  <link rel="self" type="application/atom+xml" href="http://picasaweb.google.com/data/entry/api/user/xy/albumid/531885671007533108" /> 
</entry>

Here's what I've tried:

foreach($xml->entry as $feed) {
 $album_url = $feed->xpath("./link[@rel='alternate']/@href");
 echo $album_url;
}

I've tried all kinds of permutations, too but no luck.

Expected result would be http://picasaweb.google.com/xy/Cooking

The result I get is "". Can someone explain what I'm doing wrong?

Can someone please help me out? I've been at this for hours...

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
Jay
  • 11
  • 3

2 Answers2

4

xpath() returns an array, you have to select the first element of that array, at index 0. Attention: if there's no match, it may return an empty array. Therefore, you should add an if (isset($xpath[0])) clause, just in case.

foreach ($xml->entry as $entry)
{
    $xpath = $entry->xpath('./link[@rel="alternate"]/@href');

    if (isset($xpath[0]))
    {
        echo $xpath[0], "\n";
    }
}
Josh Davis
  • 28,400
  • 5
  • 52
  • 67
1

You were close:

./link[@rel='alternate']/@href

Should be the correct XPath to get those values.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Hey Jacob, thanks for the answer... I tried to use your entry as above, but it still is returning no value. – Jay Dec 16 '09 at 04:44
  • Josh Davis's answer is correct; xpath returns multiple values. You could just be getting back an array. – Jacob Dec 16 '09 at 18:02