A bit clumsy, but it works (at least with the given example
require “rexml/document”
class REXML::Element
def same_as?(other)
self.name == other.name and
self.attribute(“id”) == other.attribute(“id”)
end
def merge(other)
if other.name == self.name
other.each_element do |child|
_merge(child)
end
end
end
def _merge(other)
if other.is_a?(REXML::Element)
if matching_node = REXML::XPath.first(self, other.name)
if matching_node.same_as?(other)
matching_node.merge(other)
else
add_element(other)
end
else
add_element(other)
end
end
end
end