I had being look for the answer left and right. Even stackoverflow only has 1 similar question, but the answer does not work in my case. I fail to validate the xml and keep getting this error:
"unable to select an element for decoding data, provide a valid 'path' argument."
My goal is converting the json data to xml with validation. Anyone has any idea?
Below is my simple code:
import xmlschema
import json
from xml.etree import ElementTree
my_xsd = '<?xml version="1.0"?><schema targetNamespace = "urn:oasis:names:tc:emergency:cap:1.2" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <element name="note" type="xs:string"/><element name="age" type="xs:integer"/> </schema>'
schema = xmlschema.XMLSchema(my_xsd)
#jdata = xmlschema.to_json(xml_document = """<note>this is a Note text</note>""", schema = schema)
#jsonData = json.dumps(jdata)
data = json.dumps({'note': 'this is a Note text','age':'5'})
#print (jdata)
#print (jsonData)
print(data)
xml = xmlschema.from_json(data, schema=schema)
ElementTree.dump(xml)
I requested help from xmlschema creator and it turns out I need to have extra parameters as: from_json(jsonTxt ,schema = CAPSchema, preserve_root=True, namespaces={'': 'urn:oasis:names:tc:emergency:cap:1.2'})