Hpricot + Ruby XML解析和邏輯選擇。Hpricot XML文本搜索
目標:查找作者Bob寫的所有標題。
我的XML文件:
<rss>
<channel>
<item>
<title>Book1</title>
<pubDate>march 1 2010</pubDate>
<author>Bob</author>
</item>
<item>
<title>book2</title>
<pubDate>october 4 2009</pubDate>
<author>Bill</author>
</item>
<item>
<title>book3</title>
<pubDate>June 5 2010</pubDate>
<author>Steve</author>
</item>
</channel>
</rss>
#my Hpricot, running this code returns no output, however the search pattern works on its own.
(doc % :rss % :channel/:item).each do |item|
a=item.search("author[text()*='Bob']")
#puts "FOUND" if a.include?"Bob"
puts item.at("title") if a.include?"Bob"
end
我從來沒有見過類似`(doc%:rss%:channel /:item)`的訪問器與Hpricot或Nokogiri一起使用。 – 2011-02-11 23:36:26