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:
<topics>
<topic>
<key>English</key>
<topic><data>CDEF</data></topic>
</topic>
<topic>
<key>Spanish</key>
<topic><data>ABC</data></topic>
</topic>
</topics>
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("<r><a><k/><b>1</b><b>2</b></a><a><k/><b>3</b></a></r>")
=> ...
irb(main):022:0> doc.xpath('//k/following-sibling::b').size
=> 3
irb(main):023:0> puts doc.xpath('//k/following-sibling::b')
<b>1</b>
<b>2</b>
<b>3</b>
=> 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]')
<b>1</b>
<b>3</b>
=> nil
following-sibling for <key>English</key> would only be the
<topic><data>CDEF</data></topic>, 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("<a><k/><b>1</b><b>2</b></a>")
=> #<Nokogiri::XML::Document:0x832daa4 name="document"
children=[#<Nokogiri::XML::Element:0x832d810 name="a"
children=[#<Nokogiri::XML::Element:0x832d68a name="k">,
#<Nokogiri::XML::Element:0x832d568 name="b"
children=[#<Nokogiri::XML::Text:0x832d450 "1">]>,
#<Nokogiri::XML::Element:0x831c02e name="b"
children=[#<Nokogiri::XML::Text:0x831bf02 "2">]>]>]>
irb(main):017:0> doc.xpath('//k/following-sibling::b').size
=> 2
irb(main):019:0> puts doc.xpath('//k/following-sibling::b')
<b>1</b>
<b>2</b>
=> nil
See also the XPath resources I mentioned earlier.
Kind regards
robert
···
2011/11/24 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
On Thu, Nov 24, 2011 at 1:49 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:
2011/11/24 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
On Thu, Nov 24, 2011 at 12:30 PM, Ruby Mania <prateek123@gmail.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/