I have the following html and like to know how to use xpath to retrieve all the info:
– Name(first, last)
– Nick Name
– email
– shipping address…
Primarily, retrieve text after <BR>. Many Thanks in advance.
<table>
<tr>
<td valign="top" width="50%" align="left">
<span>Buyer</span><br/>FirstName LastName<br/>NickName<br/>First.Last@SomeCompany.com</td>
<tr><td valign="top" width="40%" align="left">
<span><span>Shipping address - </span><span>confirmed</span></span><br/>FirstName LastName<br/>Attn: FirstName<br/>1234 Main St.<br/>TheCity, TheState, 12345<br/>United States<br/></td>
</tr></table>
After I posted the above question, I learned that I can do these, but does not look clean:
buyer = html.xpath("//span/text()[contains(., 'Buyer')]").first.parent
buyer_name = buyer.next.next
puts "Buyer's Full name: #{buyer_name.text}"
buyer_nick = buyer_name.next.next
puts "Buyer's Nick name: #{buyer_nick.text}"
buyer_email = buyer_nick.next.next
puts "Buyer's email: #{buyer_email.text}"
My question now is why the html.xpath(“//span/text()[contains(., ‘Buyer’)]”) return the TEXT itself instead of the ELEMENT. Again, thanks!!
Here’s a concise way:
The XPath does exactly what you stated: it takes all text nodes following a
br. The address is slurped into one variable, but you can get the components separately if you want.Output: