From: Robert Klemme Date: 2011-11-25T01:02:57+09:00 Subject: Re: NOKIGIRI XPATH 2011/11/24 Jesús Gabriel y Galán : > 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. With the statement above I was referring to the case where there are multiple pairs of key "English" and topic. > I mean: > > > >   >    English >    CDEF >   >   >    Spanish >    ABC >   > > > Why that xpath returns the ABC also. I would have thought that Which XPath expression are you referring to here with "that xpath"? If you mean this irb(main):020:0> doc = Nokogiri.XML("123") => ... irb(main):022:0> doc.xpath('//k/following-sibling::b').size => 3 irb(main):023:0> puts doc.xpath('//k/following-sibling::b') 1 2 3 => nil Then you get three matches but from different parents - even though you cannot distinguish them immediately. If you want to only match exactly one entry you need to add more criteria: irb(main):024:0> doc.xpath('//k/following-sibling::b[1]').size => 2 irb(main):025:0> puts doc.xpath('//k/following-sibling::b[1]') 1 3 => nil > following-sibling for English would only be the > CDEF, from which we are selecting the data > node. Generally *-sibling refers to all siblings, i.e. sub nodes of the same node irb(main):016:0> doc = Nokogiri.XML("12") => #, #]>, #]>]>]> irb(main):017:0> doc.xpath('//k/following-sibling::b').size => 2 irb(main):019:0> puts doc.xpath('//k/following-sibling::b') 1 2 => nil See also the XPath resources I mentioned earlier. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/