# How to write this xpath?

**URL:** <https://www.ruby-forum.com/t/how-to-write-this-xpath/194560>\
**Category:** Ruby\
**Created:** [September 7, 2010, 7:07am UTC](https://www.ruby-forum.com/t/how-to-write-this-xpath/194560 "2010-09-07T07:07:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 7, 2010, 7:07am UTC](https://www.ruby-forum.com/t/how-to-write-this-xpath/194560/1 "2010-09-07T07:07:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 7, 2010, 8:30am UTC](https://www.ruby-forum.com/t/how-to-write-this-xpath/194560/2 "2010-09-07T08:30:37Z")

</div>

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.zvon.org/xxl/XPathTutorial/General/examples.html)  
[http://www.w3schools.com/xpath/](http://www.w3schools.com/xpath/)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 7, 2010, 9:05am UTC](https://www.ruby-forum.com/t/how-to-write-this-xpath/194560/3 "2010-09-07T09:05:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 7, 2010, 10:18am UTC](https://www.ruby-forum.com/t/how-to-write-this-xpath/194560/4 "2010-09-07T10:18:59Z")

</div>

On Tue, Sep 7, 2010 at 9:37 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Tue, Sep 7, 2010 at 9:05 AM, Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 7, 2010, 9:37am UTC](https://www.ruby-forum.com/t/how-to-write-this-xpath/194560/5 "2010-09-07T09:37:58Z")

</div>

On Tue, Sep 7, 2010 at 9:05 AM, Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
