Skip to content

Instantly share code, notes, and snippets.

@dirkjot
Created October 12, 2017 20:44
Show Gist options
  • Save dirkjot/bd25b037b33bba6187e99d76792ceb90 to your computer and use it in GitHub Desktop.
Save dirkjot/bd25b037b33bba6187e99d76792ceb90 to your computer and use it in GitHub Desktop.
Using lmxl/etree to merge two xml trees
from lxml import etree
from copy import deepcopy
"""
This is an attempt to merge to XML trees. It works but it probably doesn't do what you want because XML elements are not only identified
by their tag. Take a POM for example, it will have many <dependency> nodes that are only distinguished by the text on their daughter
node 'GroupId'. This code does not take that into account.
"""
def treeMerge(a, b):
"""Merge two xml trees A and B, so that each recursively found leaf element of B is added to A. If the element
already exists in A, it is replaced with B's version. Tree structure is created in A as required to reflect the
position of the leaf element in B.
Given <top><first><a/><b/></first></top> and <top><first><c/></first></top>, a merge results in
<top><first><a/><b/><c/></first></top> (order not guaranteed)
"""
def inner(aparent, bparent):
for bchild in bparent:
achild = aparent.xpath('./' + bchild.tag)
if not achild:
aparent.append(bchild)
elif bchild.getchildren():
inner(achild[0], bchild)
res = deepcopy(a)
inner(res, b)
return res
lass Test_TreeMerge(unittest.TestCase):
def test_level_zero(self):
a = etree.fromstring("""<top></top>""")
b = etree.fromstring("""<top2></top2>""")
#result = donkey.treeMerge(a, b)
# should give us an error
def test_level_one(self):
a = etree.fromstring("""<top><a/><b/><d><d2/></d></top>""")
b = etree.fromstring("""<top><c/></top>""")
result = donkey.treeMerge(a, b)
self.assertEqual(len(result.xpath("/top")[0]), 4)
self.assertEqual(len(result.xpath("/top/d/d2")), 1)
def test_no_duplicates(self):
a = etree.fromstring("""<top><a/></top>""")
b = etree.fromstring("""<top><a/></top>""")
result = donkey.treeMerge(a, b)
self.assertEqual(len(result.xpath("/top")[0]), 1)
def test_level_two(self):
a = etree.fromstring("""<top><a/><b><b1/></b></top>""")
b = etree.fromstring("""<top><b><b2/></b></top>""")
result = donkey.treeMerge(a, b)
print(etree.tostring(result))
self.assertEqual(len(result.xpath("/top/a")), 1)
self.assertEqual(len(result.xpath("/top/b/b1")), 1)
self.assertEqual(len(result.xpath("/top/b/b2")), 1)
def test_level_three(self):
a = etree.fromstring("""<top><a/><b><b1><b11/><b22/></b1></b></top>""")
b = etree.fromstring("""<top><a/><b><b1><b12/><b22/></b1></b></top>""")
result = donkey.treeMerge(a, b)
print(etree.tostring(result))
self.assertEqual(len(result.xpath("/top/a")), 1)
self.assertEqual(len(result.xpath("/top/b/b1/b11")), 1)
self.assertEqual(len(result.xpath("/top/b/b1/b12")), 1)
self.assertEqual(len(result.xpath("/top/b/b1/b22")), 1)
def test_replaces_same_named_child(self):
a = etree.fromstring("""<top><b foo=1></top>""")
b = etree.fromstring("""<top><b foo=2></top>""")
result = donkey.treeMerge(a, b)
print(etree.tostring(result))
self.assertEqual(len(result.xpath("/top/b[@foo=1]")), 0)
self.assertEqual(len(result.xpath("/top/b[@foo=2]")), 1)
def deal_with_namespaces(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment