[英]XML with xpath and PHP: How to access the text value of an attribute of an entry
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>
这是我尝试过的:
foreach($xml->entry as $feed) {
$album_url = $feed->xpath("./link[@rel='alternate']/@href");
echo $album_url;
}
我也尝试过各种排列,但没有运气。
预期的结果是http://picasaweb.google.com/xy/Cooking
我得到的结果是“”。 有人可以解释我在做什么错吗?
有人可以帮我吗? 我已经在这里呆了几个小时了...
xpath()
返回一个数组,您必须选择该数组的第一个元素,索引为0。注意:如果没有匹配项,它可能会返回一个空数组。 因此,以防万一,您应该添加一个if (isset($xpath[0]))
子句。
foreach ($xml->entry as $entry)
{
$xpath = $entry->xpath('./link[@rel="alternate"]/@href');
if (isset($xpath[0]))
{
echo $xpath[0], "\n";
}
}
您接近:
./link[@rel='alternate']/@href
应该是获取那些值的正确XPath。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.