- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
您可以调用Nokogiri::XML::Node#ancestors.size
来查看节点的嵌套深度。但是有没有办法确定嵌套最深的子节点的嵌套深度呢?
或者,您如何找到从一个节点下降的所有叶节点?
最佳答案
下面的代码猴子补丁 Nokogiri::XML::Node
是为了好玩,但当然你可以将它们提取为单独的方法,如果你喜欢的话。 (只有 height
方法是您问题的一部分,但我认为 deepest_leaves
方法可能很有趣。)
require 'nokogiri'
class Nokogiri::XML::Node
def depth
ancestors.size
# The following is ~10x slower: xpath('count(ancestor::node())').to_i
end
def leaves
xpath('.//*[not(*)]').to_a
end
def height
tallest = leaves.map{ |leaf| leaf.depth }.max
tallest ? tallest - depth : 0
end
def deepest_leaves
by_height = leaves.group_by{ |leaf| leaf.depth }
by_height[ by_height.keys.max ]
end
end
doc = Nokogiri::XML "<root>
<a1>
<b1></b1>
<b2><c1><d1 /><d2><e1 /><e2 /></d2></c1><c2><d3><e3/></d3></c2></b2>
</a1>
<a2><b><c><d><e><f /></e></d></c></b></a2>
</root>"
a1 = doc.at_xpath('//a1')
p a1.height #=> 4
p a1.deepest_leaves.map(&:name) #=> ["e1", "e2", "e3"]
p a1.leaves.map(&:name) #=> ["b1", "d1", "e1", "e2", "e3"]
编辑:只是回答问题时要简洁,不要将其包装在可重复使用的部分中:
p a1.xpath('.//*[not(*)]').map{ |n| n.ancestors.size }.max - a1.ancestors.size
关于ruby - 你如何计算Nokogiri节点后代的 "levels"个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694759/
我在 OSX 上使用 RVM 和之前的 RBENV。我在 ./gems 中相对于我的项目路径安装了我的 gems 我正在使用 aws-sdk 并尝试使用 s3 客户端导致 nokogiri/nokog
我正在尝试使用以下版本在 Mac OS Big Sur 上构建 Rails 应用程序...... $ rails --version Rails 5.0.7.2 $ ruby --version ru
我正在尝试抓取 http://www.ign.com/games/reviews使用 Nokogiri,我想实例化与页面上每个游戏评论相对应的新评论对象。当然,我还想从每条评论中获取每个数字分数,并将
我正在编写 Lynda 的 Ruby on Rails 教程,一切都按预期进行。所有安装和一切都运行完美。 但是创建项目后,当我尝试启动服务器(cmd:rails server)时,我收到错误消息:
尝试按如下方式在生产模式下运行 rake Assets 预编译。 rake assets:precompile 它在 ubuntu 14.04(32 位)和 16.06(32 位)上运行良好。但是在
我需要一种方法来运行 nokogiri 脚本 #parser.rb require 'nokogiri' def parseit() //... end 并在 jruby 的 main.rb 下面运行
在解析缩进的 XML 时,不重要的空白文本节点是从结束标记和开始标记之间的空白创建的。例如,来自以下 XML: Tove Jani Reminder Don't forget me
Nokogiri 在我的 gem 文件中。 bundle 安装返回预期的输出: Using nokogiri (1.4.4) Your bundle is complete! Use `bundle
我安装了 Ruby 和 Nokogiri gem。在 irb 中,我尝试: require 'nokogiri' require 'open-uri' url = 'http://www.amazon
我想使用 XSL 将 XML 文档转换为 HTML,稍微修改一下,然后将其呈现出来。这基本上就是我正在做的: source = Nokogiri::XML(File.read 'source.xml'
我的 Ruby on Rails 应用程序使用 Rails 2.2.2 和 Ruby 1.8.7。 尝试运行时 RAILS_ENV=production rake gems:install 或任何佣金
在 IRB 中,当我键入 require“nokogiri”时,出现以下错误: LoadError: cannot load such file -- nokogiri 我的 gemfile 中安装了
我正在使用 DevKit 在 Windows 8.1 上运行全新安装的 Ruby 2.2.1。安装后我运行: gem install rails rails new testapp cd testap
我正在尝试安装 nokogiri,因为它是启动 rails 所必需的 $ rails s /usr/local/rvm/gems/ruby-1.9.3-p194@global/gems/bun
我终于设法解析了网站的部分内容: get '/' do url = '' data = Nokogiri::HTML(open(url)) @rows = data.css("td[val
我第一次使用 Nokogiri 搜索 HTML 文档。当我创建一个变量(并打印)时,它等于: beteween Nokogiri::HTML(open(url).read) 它似乎输出与完全相同的东西
如果我尝试安装 nokogiri,我会收到以下错误: rvmsudo gem install nokogiri ERROR: Error installing nokogiri: nokog
我在屏幕抓取 rake 任务中有以下代码 page = agent.get("https://domainname.co.uk/unit/27/logs?type=incoming&page=8")
我尝试(出于测试目的)解析 Google 商家 XML 提要,定义为: EasyOptic 2014-08-01T16:31:
如何将“foo”替换为“bar”? 来自 foo1foo2foo4foo5foo6 至 bar1bar2bar4bar5bar6 我只想替换标签内部内容,没有标签属性。 有什么想法吗? 最佳答案 re
我是一名优秀的程序员,十分优秀!