From: Gavin Kistner Date: 2011-09-06T13:30:02+09:00 Subject: Re: Ruby and XML On Sep 5, 2011, at 10:21 PM, Gavin Kistner wrote: > On Sep 5, 2011, at 5:42 PM, Bob Aiello wrote: >> Essentially, I have hundreds of XML files like this one that contain >> many many name value pairs. I am concerned that the value is defined >> differently (actually I have seen this) in one or more of the XML. I >> want to take an xml file and then parse the name/value pairs into a >> list. Then I want to check that list against all of the other XML in the >> system that have the same name/value pairs. > > # Find every that has a sibling > doc.xpath('//property/name[following-sibling::value]').each do |name| > value = name.at_xpath('following-sibling::value').text Upon further reflection, I'd make one minor change to my code: instead of requiring that the name key always come first in the XML, the following tweak allows for the possibility of ……: # Find all name elements that are in properties that also have a element doc.xpath('//property[value]/name').each do |name| # Find the first 'value' child in the property (assumes only one) value = name.parent.at('value').text