Skip to content

Commit 43a5ade

Browse files
added namespace support
1 parent 6dd377c commit 43a5ade

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/modelspec/utils.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ def load_xml(filename: str):
7272
root = tree.getroot() # Get the root element
7373

7474
# 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)
7679
removed_id = handle_id(data)
7780
converted_to_actual_val = convert_values(removed_id)
7881

@@ -96,11 +99,11 @@ def element_to_dict(element):
9699

97100
children_by_tag = {}
98101
for child_element in element:
99-
child_key = child_element.tag + "s"
102+
child_key = child_element.tag + 's'
100103
child_value = element_to_dict(child_element)
101104

102105
# Check if the child element has an 'id' attribute
103-
if "id" in child_element.attrib:
106+
if 'id' in child_element.attrib:
104107
# If the child element has an 'id', add it to the result dictionary directly
105108
result[child_key] = child_value
106109
else:
@@ -113,6 +116,21 @@ def element_to_dict(element):
113116
return result
114117

115118

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+
116134
def handle_id(dictionary):
117135
if isinstance(dictionary, dict):
118136
if "id" in dictionary:
@@ -224,14 +242,22 @@ def build_xml_element(data, parent=None):
224242
for child in children:
225243
child_element = build_xml_element(child)
226244
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"]):
228246
attribute_name = aattr.name
229247
attribute_value = data.__getattribute__(aattr.name)
230248
parent.set(attribute_name, str(attribute_value))
231249

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+
232257
return parent
233258

234259

260+
235261
def ascii_encode_dict(data):
236262
ascii_encode = (
237263
lambda x: x.encode("ascii")

0 commit comments

Comments
 (0)