开发者

nokogiri: wrap <tbody> around <table>'s child

how can i do this ? i need to place tbody after table tags, basically to emulate Firefox's behavior.

i done this:

nodes = @doc.css "table > *"
wrapper = nodes.wrap("<tbody&开发者_StackOverflow中文版gt;</tbody>")

Thanks.


<tbody> should be only be used to wrap the body of your table, so assuming you have no header or footer, this will work:

require 'rubygems'
require 'nokogiri'

html = Nokogiri::HTML(DATA)
html.xpath('//table').each do |htable|
  tbody = html.create_element('tbody')
  tbody.children = htable.children
  htable.children = tbody
end

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

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

prints

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜