Your requirements don't allow a general-purpose merge program (like the one you link to), but here is a program that might work for you.
Usage: ./program.py original.xml addition.xml
#! /usr/bin/python2
import sys
from lxml import etree
result = etree.Element('root')
parser = etree.XMLParser(remove_blank_text=True)
# Add each file to the tree
for xmlfile in sys.argv[1:]:
with open(xmlfile) as xmlfile:
btree = etree.parse(xmlfile, parser)
# Ensure that the resulting tree has the right root
result.tag = btree.getroot().tag
# Consider each 2nd-level item
for bchild in btree.xpath("/*/*"):
tags = result.xpath("./%s"%bchild.tag)
if len(tags) == 0:
# Add <dog-list>, for example
#print "adding %s to %s"%(bchild.tag, result.tag)
result.append(bchild)
else:
for bgrandchild in bchild:
# add <dog>, for example
#print "adding %s to %s"%(bgrandchild.tag, tags[0].tag)
tags[0].append(bgrandchild)
with open("output.xml", "w") as output:
output.write(etree.tostring(result, pretty_print = True))