tags:

views:

24

answers:

1

I am using Xpath in Ruby with following statement.

print XPath.first(Document.new(html),"//tr[@id='ctl00_c1_rr_ci_trAdd']//td[2]") 

The Query return the following text.

<td>

                1371 N Belsay Rd<br/>Burton, MI 48509
                <br/>
                <a href='http://www.mapquest.com/maps/map.adp?style=2&amp;amp;address=1371+N+Belsay+Rd&amp;amp;city=Burton&amp;amp;state=MI&amp;amp;zip=48509' class='rptLnk2' id='ctl00_c1_rr_ci_hlMapQuest' target='_blank'>See the location on a Mapquest Map</a>
                <br/>
                <a href='http://maps.google.com?q=1371+N+Belsay+Rd Burton, MI 48509' class='rptLnk2' id='ctl00_c1_rr_ci_hlGoogleMaps' target='_blank'>See the location on a Google Map</a>
            </td>

But I just want this text

1371 N Belsay Rd<br/>Burton, MI 48509

Can anyone tell me how to achieve this? When I am using scan statement - I am getting this error.

private method `scan' called for <td> ... </>:REXML::Element (NoMethodError)
A: 

An XPath expression to get this text 1371 N Belsay Rd -- as a text node, is:

((//tr[@id='ctl00_c1_rr_ci_trAdd'])//td)[2]/text()[1]

In case you want the expression to select the three nodes:

1371 N Belsay Rd<br/>Burton, MI 48509

you may use this one:

normalize-space(((//tr[@id='ctl00_c1_rr_ci_trAdd'])//td)
                              [2]
                                /node()[not(position() > 3)])
Dimitre Novatchev
It only returns `1371 N Belsay Rd` and not `1371 N Belsay Rd<br/>Burton, MI 48509`
Shubham
@Shubham: Sorry I hadn't read well your question. I have edited my answer and now you have the XPath expression that selects exactly the nodes you are asking for.
Dimitre Novatchev