How to write this xpath?

there is an html file

ok Sep 10 | Oct 10 | Dec 10
123 567
when i open it with firefox,the output is : ok Sep 10 | Oct 10 | Dec 10 123 567 what i want to get is ok Sep 10 | Oct 10 | Dec 10 here is my codes require 'rubygems' require 'nokogiri' web='/home/test' doc = Nokogiri::HTML.parse(open(web)) data=doc.xpath('/html/body/table/tr/td') puts data i get ok Sep 10 | Oct 10 | Dec 10
123 567
how can i get : ok Sep 10 | Oct 10 | Dec 10

On 07.09.2010 07:07, Pen T. wrote:

123
ok Sep 10 | Oct 10 | Dec 10
i get

how can i get : ok Sep 10 |Oct 10 |Dec 10

You want the first row? Try

‘/html/body/table/tr[1]/td’

See also
http://www.zvon.org/xxl/XPathTutorial/General/examples.html
http://www.w3schools.com/xpath/

Cheers

robert

think for your help,but your method can’t work ,i have made a try.

On Tue, Sep 7, 2010 at 9:37 AM, Robert K.
[email protected] wrote:

On Tue, Sep 7, 2010 at 9:05 AM, Pen T. [email protected] wrote:

think for your help,but your method can’t work ,i have made a try.

Ah, I see - it’s more complicated. I think it should be one of these
depending on whether you want the text nodes:

/html/body/table[1]/tr[1]/td[1]/(.|strong|a)
/html/body/table[1]/tr[1]/td[1]/(.|strong|a)/text()

but I cannot test it right now since I don’t have Nokogiri on this machine.

Now this looks awful but apparently it works:

$doc.xpath
‘/html/body/table[1]/tr[1]/td[1]/text()|/html/body/table[1]/tr[1]/td[1]/strong/text()|/html/body/table[1]/tr[1]/td[1]/a/text()’

If you want the elements you need to remove all “/text()” from the
above.

The difficult thing here is that you want to select only a portion of
the child nodes of /table/tr/td.

Kind regards

robert

On Tue, Sep 7, 2010 at 9:05 AM, Pen T. [email protected] wrote:

think for your help,but your method can’t work ,i have made a try.

Ah, I see - it’s more complicated. I think it should be one of these
depending on whether you want the text nodes:

/html/body/table[1]/tr[1]/td[1]/(.|strong|a)
/html/body/table[1]/tr[1]/td[1]/(.|strong|a)/text()

but I cannot test it right now since I don’t have Nokogiri on this
machine.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs