0

I have a very large OData XML document I'm parsing with Ruby and Nokogiri. There are multiple nodes with sub-nodes for enumerations that I want to pull into a hash.

I'm correctly finding and iterating over these nodes using:

data_xml.xpath('//Schema/EnumType').each do |enumerations|
    enumerations.xpath('//Annotation/@String').each do |enum|
       # add to my hash

I assumed by doing the second, nested, XPath search on the enumerations node set it'd only iterate over this specific node set, but the hash has every single annotation in the document, not just the ones in the node set.

Am I doing something wrong or is there a different way to apply XPath only to that sub-set?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user101289
  • 9,888
  • 15
  • 81
  • 148
  • Putting `//` at the beginning makes the xpath pattern absolute, so it doesn't "respect" the nesting. Try `enumerations.xpath('Annotation/@String')` or, better, `enumerations.xpath('.//Annotation/@String')` instead... – Konstantin Strukov Mar 13 '20 at 20:56
  • @KonstantinStrukov thank you! Please add that as an answer and I'll accept it – user101289 Mar 13 '20 at 20:58
  • See the "descendant-or-self axis (//*)" section in https://stackoverflow.com/a/35608304/128421, which discusses the various ways XPath anchors selectors. – the Tin Man Mar 14 '20 at 04:55

0 Answers0