1 """Convenience functions for handling XML.
5 # Copyright (C) 2012, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from xml
.dom
import Node
10 def _compare_children(a
, b
):
14 if ac
.length
!= bc
.length
:
17 for i
in range(ac
.length
):
18 if not nodes_equal(ac
[i
], bc
[i
]):
23 def nodes_equal(a
, b
):
24 """Compare two DOM nodes.
25 Warning: only supports documents containing elements, text nodes and attributes (will crash on comments, etc).
27 if a
.nodeType
!= b
.nodeType
:
30 if a
.nodeType
== Node
.ELEMENT_NODE
:
31 if a
.namespaceURI
!= b
.namespaceURI
:
34 if a
.nodeName
!= b
.nodeName
:
37 a_attrs
= set([(name
, value
) for name
, value
in a
.attributes
.itemsNS()])
38 b_attrs
= set([(name
, value
) for name
, value
in b
.attributes
.itemsNS()])
40 if a_attrs
!= b_attrs
:
41 #print "%s != %s" % (a_attrs, b_attrs)
44 return _compare_children(a
, b
)
45 elif a
.nodeType
in (Node
.TEXT_NODE
, Node
.CDATA_SECTION_NODE
):
46 return a
.wholeText
== b
.wholeText
47 elif a
.nodeType
== Node
.DOCUMENT_NODE
:
48 return _compare_children(a
, b
)
50 assert 0, ("Unknown node type", a
)