forked from antont/txml-xml3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcompare.py
57 lines (47 loc) · 1.32 KB
/
etcompare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
comparing two elementtrees for xml equivalence,
from http://stackoverflow.com/questions/7905380/testing-equivalence-of-xml-etree-elementtree
removed the .tail comparisons (that's the whitespace between the tags i think)
"""
def cmp_el(a,b):
#print a.tag, b.tag
#print a.tail, b.tail
#print "(0)"
if a.tag < b.tag:
return -1
elif a.tag > b.tag:
return 1
#we're only interested in the tags - not the stuff in-between
#elif a.tail < b.tail:
# return -1
#elif a.tail > b.tail:
# return 1
#print "(a)"
#compare attributes
aitems = a.attrib.items()
aitems.sort()
bitems = b.attrib.items()
bitems.sort()
if aitems < bitems:
return -1
elif aitems > bitems:
return 1
#print "(b)"
#compare child nodes
achildren = list(a)
achildren.sort(cmp=cmp_el)
bchildren = list(b)
bchildren.sort(cmp=cmp_el)
for achild, bchild in zip(achildren, bchildren):
cmpval = cmp_el(achild, bchild)
if cmpval < 0:
return -1
elif cmpval > 0:
return 1
#print "(c)"
#must be equal
return 0
def el_equal(a, b):
#for tree equivality was like this but then didn't need trees.. (yet?)
#return cmp_el(a.getroot(), b.getroot()) == 0
return cmp_el(a, b) == 0