From: "Jesús Gabriel y Galán" Date: 2011-11-25T00:32:32+09:00 Subject: Re: NOKIGIRI XPATH On Thu, Nov 24, 2011 at 1:49 PM, Robert Klemme wrote: > 2011/11/24 Jesús Gabriel y Galán : >> On Thu, Nov 24, 2011 at 12:30 PM, Ruby Mania wrote: >>> Thanks a lot for help. But it matched CDEF and all nodes after that even >>> if key != english >> >> I'm not sure why is this. I'm still trying to come up with a good >> XPath that will return just that node, > > Well, there could be many matches and from the original posting I > cannot see that only the first is needed. What I don't understand is why that xpath returns nodes whose preceding key sibling doesn't have 'English' as value. I mean: English CDEF Spanish ABC Why that xpath returns the ABC also. I would have thought that following-sibling for English would only be the CDEF, from which we are selecting the data node. >> but in the meantime you can do this: >> >> doc.xpath("//key[. = 'English']/following-sibling::topic/data")[0] > > Or better > > doc.at_xpath("//key[. = 'English']/following-sibling::topic[1]/data") > > I would probably do > > doc.xpath('//topic[preceding-sibling::key[text()="English"]]//data') > > or, for one hit only > > doc.at_xpath('//topic[preceding-sibling::key[text()="English"]][1]//data') > > Not sure about efficiency but I prefer it visually to have the path to > the selected node as basis and use criteria in [] for filtering. I agree with you, and I would guess this is more efficient, since nokogiri doesn't have to return as many nodes. Jesus.