php xpath 问题 [英] php xpath problems

查看:32
本文介绍了php xpath 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xpath 解析 blogspot 提要,但它似乎不适用于我尝试的任何内容.我不确定是因为命名空间还是什么,但我希望有人可以帮助我.代码如下:

I am trying to parse a blogspot feed using xpath but it doesnt seem to be working with anything that I try. I am not sure if it is because of the namespaces or what but I was hoping someone could help me. Here is the code:

    $xml = simplexml_load_file('http://feeds.feedburner.com/blogspot/MKuf');

$next = $xml->xpath("//link[@rel='next']");
print_r($next);

这只是返回一个空数组,它不应该是.我试过只做链接或输入,但它仍然返回空.我可以在它上面运行的唯一一个是 *.任何帮助表示赞赏.

This is just returning an empty array and it should not be. I tried it doing just link or just entry and it still is returning empty. The only one I can run on it that works is *. Any help is appreciated.

推荐答案

就像在对您的问题的评论中所说的那样,文档有一个默认命名空间,您必须先注册该命名空间,然后才能使用 XPath 对其进行查询.

Like already said in the comment to you question, the document has a default namespace which you have to register before you can query it with XPath.

由于链接的副本只展示了如何使用 DOM,我将添加一个 SimpleXml 示例

Since the linked duplicate only shows how to do it with DOM, I'll add an SimpleXml example

$feed = simplexml_load_file('http://feeds.feedburner.com/blogspot/MKuf');
$feed->registerXPathNamespace('f', 'http://www.w3.org/2005/Atom');
foreach ($feed->xpath('//f:link[@rel="next"]') as $link) {
    var_dump($link);
}

手册页:http://de.php.net/manual/de/simplexmlelement.registerxpathnamespace.php

现场演示

这篇关于php xpath 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆