我有一个博客导出的xml文件,它看起来像这样:
<feed>
<entry>
<title> title </title>
<content type="html"> Content </content>
</entry>
<entry>
<title> title </title>
<content type="html"> Content </content>
</entry>
</feed>如何使用Nokogiri和Xpath解析?
这就是我所拥有的:
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML(File.open("blogspot.xml"))
doc.xpath('//content[@type="html"]').each do |node|
puts node.text
end但是它没有给我任何东西:/
有什么建议吗?:/
发布于 2010-07-20 11:43:00
你的代码很适合我。某些版本的Nokigiri存在一些问题。
我得到了:
Content
Content我使用nokogiri (1.4.1x86-mswin32)
发布于 2010-07-20 12:01:51
结果,我不得不删除提要的属性
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>发布于 2010-10-31 04:47:42
我只是偶然发现了这个问题。问题似乎出在XML名称空间上:
“原来我不得不删除提要的属性”
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>XML名称空间使访问节点变得复杂,因为它们提供了一种分隔相似标记的方法。请阅读Searching an HTML / XML Document的“名称空间”部分。
Nokogiri也有remove_namespaces!方法,这是一种有时很有用的处理问题的方法,但也有一些缺点。
https://stackoverflow.com/questions/3286831
复制相似问题