Strip all tbody tags without destroying their children

假装没事ソ 提交于 2019-12-22 18:28:20

问题


This Ruby code using Nokogiri

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

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?


回答1:


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>

prints

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



回答2:


You want to replace each tbody with its children? Then that's all you need to say:

require 'nokogiri'
html = Nokogiri::HTML.fragment(DATA.read)
html.css('tbody').each{ |tbody| tbody.replace tbody.children }
puts html

__END__
<table><tbody>
  <tr><td>Data</td></tr>
  <tr><td>Data2</td></tr>
</tbody><tbody>
  <tr><td>Data3</td></tr>
</tbody></table>

Producing:

<table>
<tr><td>Data</td></tr>
<tr><td>Data2</td></tr>
<tr><td>Data3</td></tr>
</table>


来源:https://stackoverflow.com/questions/2696537/strip-all-tbody-tags-without-destroying-their-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!