xml_matching.py
Go to the documentation of this file.
1 import xml.dom
2 import re
3 import sys
4 
5 # regex to match whitespace
6 whitespace = re.compile(r'\s+')
7 
8 
10  if len(a.attributes) != len(b.attributes):
11  print("Different number of attributes")
12  return False
13  a_atts = [(a.attributes.item(i).name, a.attributes.item(i).value) for i in range(len(a.attributes))] # noqa
14  b_atts = [(b.attributes.item(i).name, b.attributes.item(i).value) for i in range(len(b.attributes))] # noqa
15  a_atts.sort()
16  b_atts.sort()
17 
18  for i in range(len(a_atts)):
19  if a_atts[i][0] != b_atts[i][0]:
20  print("Different attribute names: %s and %s" % (a_atts[i][0], b_atts[i][0])) # noqa
21  return False
22  try:
23  if abs(float(a_atts[i][1]) - float(b_atts[i][1])) > 1.0e-9:
24  print("Different attribute values: %s and %s" % (a_atts[i][1], b_atts[i][1])) # noqa
25  return False
26  except ValueError: # Attribute values aren't numeric
27  if a_atts[i][1] != b_atts[i][1]:
28  print("Different attribute values: %s and %s" % (a_atts[i][1], b_atts[i][1])) # noqa
29  return False
30 
31  return True
32 
33 
34 def text_matches(a, b):
35  a_norm = whitespace.sub(' ', a)
36  b_norm = whitespace.sub(' ', b)
37  if a_norm.strip() == b_norm.strip():
38  return True
39  print("Different text values: '%s' and '%s'" % (a, b))
40  return False
41 
42 
43 def nodes_match(a, b, ignore_nodes):
44  if not a and not b:
45  return True
46  if not a or not b:
47  return False
48 
49  if a.nodeType != b.nodeType:
50  print("Different node types: %s and %s" % (a, b))
51  return False
52 
53  # compare text-valued nodes
54  if a.nodeType in [xml.dom.Node.TEXT_NODE,
55  xml.dom.Node.CDATA_SECTION_NODE,
56  xml.dom.Node.COMMENT_NODE]:
57  return text_matches(a.data, b.data)
58 
59  # ignore all other nodes except ELEMENTs
60  if a.nodeType != xml.dom.Node.ELEMENT_NODE:
61  return True
62 
63  # compare ELEMENT nodes
64  if a.nodeName != b.nodeName:
65  print("Different element names: %s and %s" % (a.nodeName, b.nodeName))
66  return False
67 
68  if not all_attributes_match(a, b):
69  return False
70 
71  a = a.firstChild
72  b = b.firstChild
73  while a or b:
74  # ignore whitespace-only text nodes
75  # we could have several text nodes in a row, due to replacements
76  while (a and
77  ((a.nodeType in ignore_nodes) or
78  (a.nodeType == xml.dom.Node.TEXT_NODE and whitespace.sub('', a.data) == ""))): # noqa
79  a = a.nextSibling
80  while (b and
81  ((b.nodeType in ignore_nodes) or
82  (b.nodeType == xml.dom.Node.TEXT_NODE and whitespace.sub('', b.data) == ""))): # noqa
83  b = b.nextSibling
84 
85  if not nodes_match(a, b, ignore_nodes):
86  return False
87 
88  if a:
89  a = a.nextSibling
90  if b:
91  b = b.nextSibling
92 
93  return True
94 
95 
96 def xml_matches(a, b, ignore_nodes=[]):
97  if isinstance(a, str):
98  return xml_matches(xml.dom.minidom.parseString(a).documentElement, b,
99  ignore_nodes)
100  if isinstance(b, str):
101  return xml_matches(a, xml.dom.minidom.parseString(b).documentElement,
102  ignore_nodes)
103  if a.nodeType == xml.dom.Node.DOCUMENT_NODE:
104  return xml_matches(a.documentElement, b, ignore_nodes)
105  if b.nodeType == xml.dom.Node.DOCUMENT_NODE:
106  return xml_matches(a, b.documentElement, ignore_nodes)
107 
108  if not nodes_match(a, b, ignore_nodes):
109  print("Match failed:")
110  a.writexml(sys.stdout)
111  print()
112  print('=' * 78)
113  b.writexml(sys.stdout)
114  print()
115  return False
116  return True
def text_matches(a, b)
Definition: xml_matching.py:34
def xml_matches(a, b, ignore_nodes=[])
Definition: xml_matching.py:96
def all_attributes_match(a, b)
Definition: xml_matching.py:9
def nodes_match(a, b, ignore_nodes)
Definition: xml_matching.py:43


urdfdom_py
Author(s): Thomas Moulard, David Lu, Kelsey Hawkins, Antonio El Khoury, Eric Cousineau, Ioan Sucan , Jackie Kay
autogenerated on Mon Feb 28 2022 23:58:25