"Peter Szinek" <> wrote in message
news:...
> 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:
revious-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):
<perflog>
<module>
<perfpoints>
<variable name="202G_OrdAdd">
<variable name="203G_OrdUpdate">
....
</perfpoints
</module>
</perflog>
I tried this:
<code>
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

receding-sibling[1](@name,
"202G_OrdAdd")]')
end
</code>
In the events iterator, I also tried the following variation:
puts XPath.match(event, 'self

receding-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

receding-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:
<variable name = "203G_OrdUpdate">
<variable name = "202G_OrdAdd">
.... 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