@@ -67,18 +67,25 @@ def load_xml(filename: str):
67
67
Args:
68
68
filename: The name of the XML file to load.
69
69
"""
70
+ import re
71
+
70
72
with open (filename , "rb" ) as infile :
71
73
tree = ET .parse (infile ) # Parse the XML file into an ElementTree object
72
74
root = tree .getroot () # Get the root element
73
75
74
- # Converts the loaded xml into a string and removes unwanted string values ':ns0' and 'ns0:'
76
+ # This defines regular expressions to match the namespace patterns to be removed
77
+ ns_prefix_pattern = r"(ns\d+:|:ns\d+)"
78
+
79
+ # Converts the loaded xml into a string and removes unwanted string values ':ns0' to :ns∞ and 'ns0:' to ns∞:
75
80
# They prevent the xml from loading correctly
76
- xml_string = ET .tostring (root ).decode ().replace ('ns0:' , '' ).replace (':ns0' , '' ).strip ()
81
+ xml_string = ET .tostring (root ).decode ()
82
+ cleaned_xml = re .sub (ns_prefix_pattern , "" , xml_string ).strip ()
77
83
78
84
# Removes xmlns, xmlns:xsi and xsi:schemaLocation from the xml structure for conversion
79
- removed_namespaces = process_xml_namespace (xml_string )
85
+ # it passes an element tree object to the element_to_dict function
86
+ removed_namespaces = process_xml_namespace (cleaned_xml )
80
87
81
- # Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
88
+ # Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
82
89
data = element_to_dict (removed_namespaces )
83
90
84
91
# Removes every key having 'id' and replaces it with it's value
@@ -107,11 +114,11 @@ def element_to_dict(element):
107
114
108
115
children_by_tag = {}
109
116
for child_element in element :
110
- child_key = child_element .tag + 's'
117
+ child_key = child_element .tag + "s"
111
118
child_value = element_to_dict (child_element )
112
119
113
120
# Check if the child element has an 'id' attribute
114
- if 'id' in child_element .attrib :
121
+ if "id" in child_element .attrib :
115
122
# If the child element has an 'id', add it to the result dictionary directly
116
123
result [child_key ] = child_value
117
124
else :
@@ -129,12 +136,12 @@ def process_xml_namespace(xml_string):
129
136
ignored_elements = [
130
137
'xmlns="http://www.neuroml.org/schema/neuroml2"' ,
131
138
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' ,
132
- 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"'
139
+ 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"' ,
133
140
]
134
141
135
142
# Loops through the xml string and removes every instance of the elements in the list named ignored_elements
136
143
for ignored_element in ignored_elements :
137
- xml_string = xml_string .replace (ignored_element , '' ).strip ()
144
+ xml_string = xml_string .replace (ignored_element , "" ).strip ()
138
145
139
146
# Parse the XML string into an ElementTree
140
147
root = ET .fromstring (xml_string )
@@ -254,23 +261,27 @@ def build_xml_element(data, parent=None):
254
261
for child in children :
255
262
child_element = build_xml_element (child )
256
263
parent .append (child_element )
257
- elif not any (hasattr (data , attr_name ) for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]):
264
+
265
+ # Filters name space and schemaLoacation attributes, only allows non name space attributes to added as attributes
266
+ elif not any (
267
+ hasattr (data , attr_name )
268
+ for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]
269
+ ):
258
270
attribute_name = aattr .name
259
271
attribute_value = data .__getattribute__ (aattr .name )
260
272
parent .set (attribute_name , str (attribute_value ))
261
-
273
+
262
274
# This defines the various namespaces and schemaLocation of the generated xml
263
275
if hasattr (data , "xmlns" ):
264
276
parent .set ("xmlns" , data .xmlns )
265
277
if hasattr (data , "xmlns_url" ):
266
278
parent .set ("xmlns:xsi" , data .xmlns_url )
267
279
if hasattr (data , "xmlns_loc" ):
268
- parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + ' \n ' + data .xmln_loc_2 ))
280
+ parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + " \n " + data .xmln_loc_2 ))
269
281
270
282
return parent
271
283
272
284
273
-
274
285
def ascii_encode_dict (data ):
275
286
ascii_encode = (
276
287
lambda x : x .encode ("ascii" )
0 commit comments