# Merging two REXML element-lists

**URL:** <https://www.ruby-forum.com/t/merging-two-rexml-element-lists/97942>\
**Category:** Ruby\
**Created:** [July 11, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/merging-two-rexml-element-lists/97942 "2006-07-11T16:48:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejepsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@ejepsen](https://www.ruby-forum.com/u/ejepsen)\
**Post date:** [July 11, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/merging-two-rexml-element-lists/97942/1 "2006-07-11T16:48:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ejepsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@ejepsen](https://www.ruby-forum.com/u/ejepsen)\
**Post date:** [July 11, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/merging-two-rexml-element-lists/97942/2 "2006-07-11T19:13:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ejepsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@ejepsen](https://www.ruby-forum.com/u/ejepsen)\
**Post date:** [July 12, 2006, 12:15am UTC](https://www.ruby-forum.com/t/merging-two-rexml-element-lists/97942/3 "2006-07-12T00:15:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ejepsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@ejepsen](https://www.ruby-forum.com/u/ejepsen)\
**Post date:** [July 12, 2006, 1:22am UTC](https://www.ruby-forum.com/t/merging-two-rexml-element-lists/97942/4 "2006-07-12T01:22:15Z")

</div>

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.”
