i want to make sure all table's immediate child is tbody....
how can i write this with xpath or nokogiri ?
doc.search("//table/").each do |j|
new_parent = Nokogiri::XML::Node.new('tbody',doc)
j.replace new_parent
new_parent << j
end
i want to make sure all table's immediate child is tbody....
how can i write this with xpath or nokogiri ?
doc.search("//table/").each do |j|
new_parent = Nokogiri::XML::Node.new('tbody',doc)
j.replace new_parent
new_parent << j
end
require 'rubygems'
require 'nokogiri'
html = Nokogiri::HTML(DATA)
html.xpath('//table').each do |table|
tbody = Nokogiri::XML::Node.new('tbody', html)
table.children.each do |child|
if child.name.downcase == 'tbody'
child.children.each do |grandchild|
grandchild.parent = tbody
end
child.remove
else
child.parent = tbody
end
end
table.add_child(tbody)
end
puts html.xpath('//table').to_s
__END__
<table border="0" cellspacing="5" cellpadding="5">
<tr><th>Header</th></tr>
<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"><tbody>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><td>Data2</td></tr>
<tr><td>Data3</td></tr>
</tbody></table>