Skip to content

Commit

Permalink
fixing handling attributes in XML namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Mar 25, 2021
1 parent 356248f commit bf4ab83
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion biosimulators_utils/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.42'
__version__ = '0.1.43'
5 changes: 4 additions & 1 deletion biosimulators_utils/sedml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ def get_value_of_variable_model_xml_targets(variable, model_etrees):
if len(obj) != 1:
raise ValueError('xpath {} must match a single object in model {}'.format(obj_xpath, variable.model.id))

ns, _, attr = attr.rpartition(':')
if ns:
attr = '{{{}}}{}'.format(variable.target_namespaces[ns], attr)

value = obj[0].get(attr)
if value is None:
raise ValueError('Target `{}` is not defined in model `{}`.'.format(variable.target, variable.model.id))
Expand Down Expand Up @@ -1070,7 +1074,6 @@ def get_xml_node_namespace_tag_target(etree, target_namespaces=None):
* :obj:`str`: tag
* :obj:`str`: target for use with SED target XPATHs
* :obj:`dict`: dictionary that maps the prefixes of namespaces to their URIs
* :obj:`str`, optional: default namespace prefix
"""
target_namespaces = dict(target_namespaces or {})
uri, _, tag = etree.tag.rpartition('}')
Expand Down
7 changes: 6 additions & 1 deletion tests/sedml/test_sedml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def test_apply_compute_model_change_new_value(self):
model_filename = os.path.join(self.tmp_dir, 'model_1.xml')
with open(model_filename, 'w') as file:
file.write('<model>')
file.write('<parameter id="x" value="2.0" strValue="a value" />')
file.write('<parameter id="x" value="2.0" strValue="a value" qual:attrA="2.3" xmlns:qual="https://qual.sbml.org" />')
file.write('<parameter id="y" value="3.0" />')
file.write('</model>')
models = {
Expand Down Expand Up @@ -761,6 +761,11 @@ def test_apply_compute_model_change_new_value(self):
self.assertEqual(utils.get_value_of_variable_model_xml_targets(change.variables[0], models), 2.0)
self.assertEqual(utils.get_value_of_variable_model_xml_targets(change.variables[1], models), 3.0)

var = data_model.Variable(id='var', model=data_model.Model(id='model_1'),
target="/model/parameter[@id='x']/@qual:attrA",
target_namespaces={'qual': 'https://qual.sbml.org'})
self.assertEqual(utils.get_value_of_variable_model_xml_targets(var, models), 2.3)

doc = data_model.SedDocument(models=[change.variables[0].model, change.variables[1].model])

change.variables[0].model.source = 'https://models.com/model_1.xml'
Expand Down

0 comments on commit bf4ab83

Please sign in to comment.