Merging two REXML element-lists

Hello,

I have two xml files which are identical almost identical. #1 holds all
possible values to the application. #2 holds a selected subset of values
which should overwrite the values of the first where identical.

ie.
#1

<?xml version="1.0" encoding="UTF-8"?>












#2

<?xml version="1.0" encoding="UTF-8"?>











What I need is a result-set that looks like this that I can do some
xpath on…

<?xml version="1.0" encoding="UTF-8"?>













In general what has happend is that the two results have merged. Results
from #2 have overwritten #1 where identical.

It looks like a rather simple “Merge” to me but I can’t figure out how
to do this in RMXML

I would very much appreciate your help! Thank you

//Erling

Erling Jepsen wrote:

Hello,

I have two xml files which are identical almost identical. #1 holds all
possible values to the application. #2 holds a selected subset of values
which should overwrite the values of the first where identical.

You could iterate over the second list and use the value of the ‘key’
attribute to construct an XPath to the matching item in the first list.

Then either replace the elements, or grab the value and use it update
the other list.


James B.

“Judge a man by his questions, rather than his answers.”

  • Voltaire

Could you supply a bit of code/meta-code to prove your point?

  • that would be much appreciated!

thank you

//Erling

James B. wrote:

You could iterate over the second list and use the value of the ‘key’
attribute to construct an XPath to the matching item in the first list.

Then either replace the elements, or grab the value and use it update
the other list.


James B.

“Judge a man by his questions, rather than his answers.”

  • Voltaire

Erling Jepsen wrote:

Could you supply a bit of code/meta-code to prove your point?

  • that would be much appreciated!

Assume the XML is in doc1 and doc2, with 2 holding the selected subset.

doc2.elements.to_a("//setting").each do |setting|
key = setting.attributes[‘key’]
value = setting.attributes[‘value’]
other_el = doc1.elements.to_a("//setting[@key=’#{key}]").first
other_el.attributes[‘value’] = value
end

Or something like that


James B.

“People want simple stories.”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs