From: Jeff Nyman Date: 2006-10-06T21:25:15+09:00 Subject: Re: Parse and modify an XML file with REXML "Peter Szinek" wrote in message news:45257DBC.20609@rubyrailways.com... > jeffnyman@gmail.com wrote: >> Greetings all. >> >> When processing XML, is there a way to check what the previous and what >> the next "rows" are? > > I don't know REXML that much (and using Hpricot anyway ;-) but standard > XPath axes ( following-sibling, preceding-sibling ) won't help? The > previous node in this case would be self::previous-sibling[1] etc. Thanks for the suggestion. This sounds like it might work. I did not see this in the REXML documentation initially but I see generally how these work in concept. In practice, it does not seem to work for me. I have my XML like this (greatly pared down): .... I tried this: xml = Document.new(File.open("test.xml")) events = XPath.match(xml, '/perflog/module/perfpoints/variable[@name="203G_OrdUpdate"]' ) events.each do |event| puts XPath.match(event, '[self:preceding-sibling[1](@name, "202G_OrdAdd")]') end In the events iterator, I also tried the following variation: puts XPath.match(event, 'self:preceding-sibling[1](@name, "202G_OrdAdd")') I also tried replacing the 'self' with the full node path (i.e., "//perflog/module/perfpoints/variable"). I should note I don't get an error when I run the above. I simply get nothing, so my guess is that I'm using preceding-sibling wrong. I'm guessing it never feels it found the condition I'm indicating it should be finding. I did find that I can do this: puts XPath.match(event, '[self:preceding-sibling::variable[1](@name, "202G_OrdAdd")]') (Note the "::variable[1]" addition.) Some documentation I found suggests that this should count backwards and reference the closest preceding variable sibling. That does seem to work -- to an extent, but I get everything returned. Meaning I get this in my results: ... but then I get all the other 203's in my XML listed as well. What I'm trying to do is just return the one 203 that has a preceding sibling that has the attribute name 202G_OrdAdd. I'm getting closer, though. Thank you for the suggestion, as this does seem to be the road I need to be on. - Jeff