剥离所有 tbody 标签而不破坏他们的孩子 [英] Strip all tbody tags without destroying their children

查看:45
本文介绍了剥离所有 tbody 标签而不破坏他们的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此 Ruby 代码使用 Nokogiri

This Ruby code using Nokogiri

doc.xpath("//tbody").remove

删除 的子项(以及 本身).我只想从文档中删除所有 <tbody> 标签,让他们的孩子留在原地.我怎样才能做到这一点?

removes the children of the <tbody> (as well as the <tbody> themselves). I only want to remove all <tbody> tags from the document, leaving their children in place. How can I achieve this?

推荐答案

require 'rubygems'
require 'nokogiri'

html = Nokogiri::HTML(DATA)
html.xpath('//table/tbody').each do |tbody|
  tbody.children.each do |child|
    child.parent = tbody.parent
  end
  tbody.remove
end

puts html.xpath('//table').to_s

__END__
<table border="0" cellspacing="5" cellpadding="5"><tbody>
<tr><td>Data</td></tr>
<tr><td>Data2</td></tr>
<tr><td>Data3</td></tr>
</tbody></table>

印刷品

<table border="0" cellspacing="5" cellpadding="5">
<tr><td>Data</td></tr>
<tr><td>Data2</td></tr>
<tr><td>Data3</td></tr>
</table>

这篇关于剥离所有 tbody 标签而不破坏他们的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆