XSD Enumeration decodes but does not re-encode due to namespaces
Version: xmlschema 3.3.1
I encountered an issue where I am unable to encode an XSD enumeration which utilizes namespaces (which had just been decoded). The code complains that s:Sender must belong to a set of values, but this set of values includes {http://www.w3.org/2003/05/soap-envelope}Sender. The xml as shown by the error has the namespace xmlns:s="http://www.w3.org/2003/05/soap-envelope". As a side-note the .xsd schema file denotes this namespace by tns rather than s.
File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/groups.py", line 1198, in iter_encode
for result in xsd_element.iter_encode(value, validation, **kwargs):
File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 1080, in iter_encode
yield self.validation_error(validation, e, elem, **kwargs)
File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/xsdbase.py", line 249, in validation_error
raise error
File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/simple_types.py", line 1475, in iter_encode
validator(obj)
File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/facets.py", line 633, in __call__
raise XMLSchemaValidationError(self, value, reason)
xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating 's:Sender' with XsdEnumerationFacets(['{http://www.w3.org/2003/05/soap-envelope}DataEncodingUnknown', '{http://www.w3.org/2003/05/soap-envelope}MustUnderstand', '{http://www.w3.org/2003/05/soap-envelope}Receiver', '{http://www.w3.org/2003/05/soap-envelope}Sender', '{http://www.w3.org/2003/05/soap-envelope}VersionMismatch']):
Reason: value must be one of ['{http://www.w3.org/2003/05/soap-envelope}DataEncodingUnknown', '{http://www.w3.org/2003/05/soap-envelope}MustUnderstand', '{http://www.w3.org/2003/05/soap-envelope}Receiver', '{http://www.w3.org/2003/05/soap-envelope}Sender', '{http://www.w3.org/2003/05/soap-envelope}VersionMismatch']
Schema component:
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="tns:DataEncodingUnknown" />
Instance type: <class 'xml.etree.ElementTree.Element'>
Instance:
<s:Value xmlns:s="http://www.w3.org/2003/05/soap-envelope">s:Sender</s:Value>
Hi, the XSD enumeration has a xs:QName as base type, so prefixed values have to be converted to expanded form (the prefix can change ...).
The fix for this is to provide the expanded form to facets validators in case of type derived from xs:QName or xs:NOTATION also during the encode phase:
if self.validators:
if self.root_type.name in (XSD_QNAME, XSD_NOTATION):
value = get_extended_qname(obj, kwargs.get('namespaces'))
else:
value = obj
for validator in self.validators:
try:
validator(value)
except XMLSchemaValidationError as err:
yield err
that fix will be included in the next release of the package.
thank you
Cool, thanks for that!
Hi, release v3.4.2 with the latest release of elementpath should resolve the encoding of QNames (by improving the decode of XSD datatypes and taking the namespace information from the element nodes).