@@ -72,7 +72,10 @@ def load_xml(filename: str):
72
72
root = tree .getroot () # Get the root element
73
73
74
74
# Convert the ElementTree object to a dictionary
75
- data = element_to_dict (root )
75
+ xml_string = ET .tostring (root ).decode ().replace ('ns0:' , '' ).replace (':ns0' , '' ).strip ()
76
+
77
+ removed_namespaces = process_xml_namespace (xml_string )
78
+ data = element_to_dict (removed_namespaces )
76
79
removed_id = handle_id (data )
77
80
converted_to_actual_val = convert_values (removed_id )
78
81
@@ -96,11 +99,11 @@ def element_to_dict(element):
96
99
97
100
children_by_tag = {}
98
101
for child_element in element :
99
- child_key = child_element .tag + "s"
102
+ child_key = child_element .tag + 's'
100
103
child_value = element_to_dict (child_element )
101
104
102
105
# Check if the child element has an 'id' attribute
103
- if "id" in child_element .attrib :
106
+ if 'id' in child_element .attrib :
104
107
# If the child element has an 'id', add it to the result dictionary directly
105
108
result [child_key ] = child_value
106
109
else :
@@ -113,6 +116,21 @@ def element_to_dict(element):
113
116
return result
114
117
115
118
119
+ def process_xml_namespace (xml_string ):
120
+ # Remove ignored elements from the XML string
121
+ ignored_elements = [
122
+ 'xmlns="http://www.neuroml.org/schema/neuroml2"' ,
123
+ 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' ,
124
+ 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"'
125
+ ]
126
+ for ignored_element in ignored_elements :
127
+ xml_string = xml_string .replace (ignored_element , '' ).strip ()
128
+
129
+ # Parse the XML string into an ElementTree
130
+ root = ET .fromstring (xml_string )
131
+ return root
132
+
133
+
116
134
def handle_id (dictionary ):
117
135
if isinstance (dictionary , dict ):
118
136
if "id" in dictionary :
@@ -224,14 +242,22 @@ def build_xml_element(data, parent=None):
224
242
for child in children :
225
243
child_element = build_xml_element (child )
226
244
parent .append (child_element )
227
- else :
245
+ elif not any ( hasattr ( data , attr_name ) for attr_name in [ "xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]) :
228
246
attribute_name = aattr .name
229
247
attribute_value = data .__getattribute__ (aattr .name )
230
248
parent .set (attribute_name , str (attribute_value ))
231
249
250
+ if hasattr (data , "xmlns" ):
251
+ parent .set ("xmlns" , data .xmlns )
252
+ if hasattr (data , "xmlns_url" ):
253
+ parent .set ("xmlns:xsi" , data .xmlns_url )
254
+ if hasattr (data , "xmlns_loc" ):
255
+ parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + '\n ' + data .xmln_loc_2 ))
256
+
232
257
return parent
233
258
234
259
260
+
235
261
def ascii_encode_dict (data ):
236
262
ascii_encode = (
237
263
lambda x : x .encode ("ascii" )
0 commit comments