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"?>
<instance>
  <environments>
    <environment range="all">
      <configfiles>
        <configfile filename="%instance%.rsp">
         <setting stanza="[webseal-config]" key="-name" value="My
Instance"/>
         <setting stanza="[webseal-config]" key="-username"
value="my_user"/>
         <setting stanza="[webseal-config]" key="-password"
value="dummy-pwd"/>
        </configfile>
      </configfiles>
    </environment>
  </environments>
</instance>

···

-------

--#2---
<?xml version="1.0" encoding="UTF-8"?>
<instance>
  <environments>
    <environment range="all">
      <configfiles>
        <configfile filename="%instance%.rsp">
         <setting stanza="[webseal-config]" key="-password"
value="secret123"/>
         <setting stanza="[webseal-config]" key="-host"
value="main_server"/>
        </configfile>
      </configfiles>
    </environment>
  </environments>
</instance>
------

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"?>
<instance>
  <environments>
    <environment range="all">
      <configfiles>
        <configfile filename="%instance%.rsp">
         <setting stanza="[webseal-config]" key="-name" value="My
Instance"/>
         <setting stanza="[webseal-config]" key="-username"
value="my_user"/>
         <setting stanza="[webseal-config]" key="-password"
value="secret123"/>
         <setting stanza="[webseal-config]" key="-host"
value="main_server"/>
        </configfile>
      </configfiles>
    </environment>
  </environments>
</instance>
------

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

--
Posted via http://www.ruby-forum.com/.

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 Britt

"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 Britt 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 Britt

"Judge a man by his questions, rather than his answers."
  - Voltaire

--
Posted via http://www.ruby-forum.com/\.

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 Britt

"People want simple stories."