From: Robert Klemme Date: 2010-10-21T00:24:02+09:00 Subject: Re: Help missing something BASIC On Wed, Oct 20, 2010 at 4:14 PM, Don Norcott wrote: > Thanks Robert > > I now have the code working with one additional line. > > require 'open-uri' > require 'nokogiri' > > doc = Nokogiri::HTML(open("c:/RUBY_OUT.TXT")); > doc.xpath("//table[@class='result']").each do |node| > > # next line has been added. >    doc2 = Nokogiri::HTML("" << "#{node}" << "") I'm sorry but this is ridiculous! >    puts "*************" >    puts doc2.xpath("//td[@class='itemNumbr']") >    puts "*************" > end > > I realized (even though I can not figure out why) early on that I had to > save off each table in the "do" before processing it to get around this > problem.  I tried many things including an array which worked fine to > save the s but I could not xpath the saved
s. As I said: you need a *relative XPath*. Your problem is the global XPath. You need to shave off the leading "//" or prefix it with ".". doc = Nokogiri::HTML(open("c:/RUBY_OUT.TXT")); doc.xpath("//table[@class='result']").each do |node| puts "*************" puts node.xpath("td[@class='itemNumbr']") puts "*************" end doc = Nokogiri::HTML(open("c:/RUBY_OUT.TXT")); doc.xpath("//table[@class='result']").each do |node| puts "*************" puts node.xpath(".//td[@class='itemNumbr']") puts "*************" end > What I did not realize that if I took the
raw it was no longer > valid XML. What twigged me is your code adding in to give the > correct html header > > Now if you can shed some light on my underlying question. > Above I embed the node in a new html object,  the new object contains > nothing other than a single
. > > Also if print the node it contains only a single
. > > Yet if I attempt to execute > "puts node.xpath("//td[@class='itemNumbr']")" > it finds the "itemnumbr" for all
items even though they do not > exist in node.  Does node actually contain the entire html, just not > visible. http://www.w3schools.com/xpath/xpath_syntax.asp Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/