Hi as the title suggest I am trying to write/edit xml using xsd with xmlschema. I am using this xsd (https://github.com/NREL/bcl-gem/blob/develop/schemas/v3/measure_v3.xsd) and this xml file (https://drive.google.com/file/d/1WKJVBjn6IjmO-EZX9yGC8AaTCynUugFC/view?usp=sharing)
schema = xmlschema.XMLSchema(xsd_path)
print(schema.is_valid(xml_path))
d = xmlschema.to_json(xml_path, schema=schema)
d = schema.to_dict(xml_path)
print(d)
json_data = json.dumps(d)
xml = xmlschema.from_json(json_data, schema=schema, preserve_root=True)
However I keep getting this error
File "/home/lib/python3.12/site-packages/xmlschema/validators/schemas.py", line 2245, in encode
for result in self.iter_encode(obj, path, validation, *args, **kwargs):
File "/home/lib/python3.12/site-packages/xmlschema/validators/schemas.py", line 2229, in iter_encode
raise XMLSchemaEncodeError(self, obj, self.elements, reason, namespaces=namespaces)
xmlschema.validators.exceptions.XMLSchemaEncodeError: failed validating <class 'dict'> instance with XMLSchema10(name='measure_v3.xsd', namespace=''):
Reason: unable to select an element for decoding data, provide a valid 'path' argument.
The issue is very similar to (https://github.com/sissaschool/xmlschema/issues/241) and (Fail to use xmlschema.from_json). In these posts it was suggested that.
it's a matter of namespaces, despite there is no prefix the data are still bound to namespace of the schema:
One can get the namespace from the xsd by using the command. However, the xsd provided to me does not have a target_namespace.
>>> CAPSchema.target_namespace
'urn:oasis:names:tc:emergency:cap:1.2'
I cannot figure out how to translate the solutions given for my case. I am not very familiar with xml and xsd, hope I am not missing something that is very obvious here. Thanks
I have gotten an answer from xmlschema github repo (https://github.com/sissaschool/xmlschema/issues/395). I will post a working script here hope it will be useful for others who might have the same issue:
import xmlschema
import json
from xml.etree.ElementTree import ElementTree
xsd_path = '/measure_v3.xsd'
xml_path = '/measure.xml'
res_path = '/measure_edited.xml'
schema = xmlschema.XMLSchema(xsd_path)
print(schema.is_valid(xml_path))
d = schema.to_dict(xml_path, preserve_root=True)
d['measure']['arguments'] = {'argument': [{'name': 'srf_temps',
'display_name': 'Surface Temperatures',
'description': 'Output the surface temperatures of each surface',
'type': 'Boolean', 'required': True, 'model_dependent': False}]}
json_data = json.dumps(d)
xml = xmlschema.from_json(json_data, schema=schema, preserve_root=True, path='measure')
ElementTree(xml).write(res_path)