tags:

views:

26

answers:

1

i want to get row which it contains more than 3 columns how to write xpath with nokogiri

require 'rubygems' require 'nokogiri' item='sometext' doc = Nokogiri::HTML.parse(open(item)) data=doc.xpath('/html/body/table/tr[@td.size>3]') puts data it can not run , help and advices appreciated.

A: 

The correct XPath will be something like this.

doc.xpath('/html/body/table/tr[count(td)>3]')

However, in my test program, I can't get Nokogiri to like absolute XPaths like this. I have to use the double-slash XPath instead.

require 'rubygems'
require 'nokogiri'

html = %{
<table>
<tr class=wrong><td><td></tr>
<tr class=right><td><td><td></tr>
</table>
}

doc = Nokogiri::HTML.parse(html)
data = doc.xpath('//table/tr[count(td)>2]')
puts data.attribute('class')
AboutRuby