2011-01-31 68 views

回答

2
biggest_table = doc.xpath('//table').max_by do |table| 
    table.xpath('.//tr').length 
end 

或者,如果有一条领带,也许你想用最行的所有表的列表:

# Hash mapping number of rows to array of table nodes 
tables = doc.xpath('//table').group_by{ |t| t.xpath('.//tr').length } 

# Array of all (possibly only 1) tables with the most rows 
biggest_n = tables[table.keys.max] 
+0

感谢Phrogz。我现在把它作为一个单线程``largest_table = doc.xpath('// table')。max_by {| table | table.xpath('.// tr')。length}` – 2011-01-31 16:30:08

0

这可能不是您要查找的内容,但您可以在浏览器中使用jQuery或Prototype轻松完成此操作。

0
tables = @doc.xpath('//table') 
tr_count = tables.map{|n| n.xpath('tr|*/tr').length} 
biggest_table = tables[tr_count.index(tr_count.max)]