I would like to go through all the elements <HeadA>and <HeadB>in the XML file and add to it a unique identifier. The approach I've tried so far:
@xml.each_element('//HeadA | //HeadB') do |heading|
end
The problem is that the node set from XPath //HeadA | //HeadBis everything HeadAfollowed by all HeadBs. I need an ordered list of everyone HeadAand HeadBin the order in which they appear in the document.
To clarify, my XML might look like this:
<Doc>
<HeadA>First HeadA</HeadA>
<HeadB>First HeadB</HeadB>
<HeadA>Second HeadA</HeadA>
<HeadB>Second HeadB</HeadB>
</Doc>
And what I get from XPath:
<HeadA>First HeadA</HeadA>
<HeadA>Second HeadA</HeadA>
<HeadB>First HeadB</HeadB>
<HeadB>Second HeadB</HeadB>
when i need to get the nodes in order:
<HeadA>First HeadA</HeadA>
<HeadB>First HeadB</HeadB>
<HeadA>Second HeadA</HeadA>
<HeadB>Second HeadB</HeadB>
so I can add identifiers sequentially.
source
share