【发布时间】:2015-09-07 20:55:35
【问题描述】:
我有一个具有以下树结构的 XML 文件。
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Videos</title>
<link>https://www.example.com/r/videos/</link>
<description>A long description of the video.</description>
<image>...</image>
<atom:link rel="self" href="http://www.example.com/videos/.xml" type="application/rss+xml"/>
<item>
<title>The most used Jazz lick in history.</title>
<link>
http://www.example.com/
</link>
<guid isPermaLink="true">
http://www.example.com/
</guid>
<pubDate>Mon, 07 Sep 2015 14:43:34 +0000</pubDate>
<description>
<table>
<tr>
<td>
<a href="http://www.example.com/">
<img src="http://www.example.com/.jpg" alt="The most used Jazz lick in history." title="The most used Jazz lick in history." />
</a>
</td>
<td> submitted by
<a href="http://www.example.com/"> jcepiano </a>
<br/>
<a href="http://www.youtube.com/">[link]</a>
<a href="http://www.example.com/">
[508 comments]
</a>
</td>
</tr>
</table>
</description>
<media:title>The most used Jazz lick in history.</media:title>
<media:thumbnail url="http://example.jpg"/>
</item>
</channel>
</rss>
这里,html table 元素嵌入在 XML 中,这让我很困惑。
现在我想选择//channel/item/title 的文本节点值和//channel/item/description/table/tr/td[1]/a[1] 的href 值(带有文本节点value = "[link]")
在第二种情况下,我正在寻找第二个a的值(带有文本节点value = "[link]"),在第二个td里面tr,table,description,item , channel.
我正在使用 PHP DOMDocument();
这两天我一直在寻找一个完美的解决方案,你能告诉我这是怎么发生的吗?
我还需要计算提要中的项目总数,现在我正在这样做:
...
$queryResult = $xpathvar->query('//item/title');
$total = 1;
foreach($queryResult as $result){
$total++;
}
echo $title;
我还需要一个 XPath 查询选择器规则的参考链接。
提前致谢! :)
【问题讨论】:
-
通过在标记名等内联代码元素周围使用
``(反引号),可以更轻松地阅读和剖析英文文本中的代码。我已经更新了你的问题。 -
感谢您的帮助和信息,下次我会记住的。