It would be useful to distinguish between Node (a specific piece of structured XML data at a specific location in the tree) and the node template, which is the data structure.
Nokogiri (and most other XML libraries) allows you to specify Node s patterns, not nodes. Therefore, when you created price = Nokogiri::XML::Node.new "price", @items , you had a specific piece of data that belongs to a specific location but did not yet define a location.
When you added it to the first <item> , you determined its place. When you added it to the second <item> , you pulled it out of your place and placed it in a new place. At this moment, this Node appeared only in the second <item> . This continues when you add the same Node to each element until you reach the last <item> where the node is located.
Nokogiri has no way to specify a node pattern. What you need to do:
@items.xpath('//items/item/manufacturer').each do |node| price = Nokogiri::XML::Node.new "price", @items price.content = "10" node.add_next_sibling(price) end
Ken bloom
source share