简单的 xpath 问题让我发疯 [英] Simple xpath question that drives me crazy
本文介绍了简单的 xpath 问题让我发疯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我设法使用此 xpath 打印内容的提要结构$xml->xpath('/rss/channel//item')
结构
<;/link><author></author></item></channel></rss>
但是我的一些文件遵循这种结构
<feed xmlns="http://www.w3.org/2005/Atom" .....><entry><published></published><title></title><description></description><link></link><author></author></entry></feed>
我猜这应该是获取条目内容的xpath
$xml->xpath('/feed//entry')
证明我错的东西.
我的问题是什么是正确的 xpath 使用?我还缺什么吗?
这是代码
xpath('/feed//entry'));}echo "";打印_r($条目);回声"</pre>";?>
解决方案
试试这个:
$xml->registerXPathNamespace('f', 'http://www.w3.org/2005/Atom');$xml->xpath('/f:feed/f:entry');
below is the structure of a feed I managed to print the content using this xpath
$xml->xpath('/rss/channel//item')
the structure
<rss><channel><item><pubDate></pubDate><title></title><description></description><link></link><author></author></item></channel></rss>
However some of my files follow this structure
<feed xmlns="http://www.w3.org/2005/Atom" .....><entry><published></published><title></title><description></description><link></link><author></author></entry></feed>
and I guessed that this should be the xpath to get the content of entry
$xml->xpath('/feed//entry')
something that proved me wrong.
My question is what is the right xpath to use? Am i missing something else ?
This is the code
<?php
$feeds = array('http://feeds.feedburner.com/blogspot/wSuKU');
$entries = array();
foreach ($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath('/feed//entry'));
}
echo "<pre>"; print_r($entries); echo"</pre>";
?>
解决方案
try this:
$xml->registerXPathNamespace('f', 'http://www.w3.org/2005/Atom');
$xml->xpath('/f:feed/f:entry');
这篇关于简单的 xpath 问题让我发疯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文