From: Robert Klemme Date: 2011-11-24T21:49:32+09:00 Subject: Re: NOKIGIRI XPATH 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. > 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. If we want to be even more robust we could do doc.xpath('//topic[preceding-sibling::key[last() and text()="English"]]//data') This will avoid matching the topic in English ... Foo ... or English ... Kind regards robert PS: My favorite XPath help http://www.w3schools.com/xpath/default.asp http://www.zvon.org/xxl/XPathTutorial/General/examples.html -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/