Change get_context() in document module

- Now the optional schema argument is a fallbak in case
    the schema path is not found into XML resource.
This commit is contained in:
Davide Brunato 2019-09-23 17:29:08 +02:00
parent 8d56d128ca
commit b6c6e2ac8f
5 changed files with 15 additions and 9 deletions

View File

@ -25,12 +25,16 @@ def get_context(source, schema=None, cls=None, locations=None, base_url=None,
if cls is None:
cls = XMLSchema
if schema is None:
try:
schema, locations = fetch_schema_locations(source, locations, base_url=base_url)
except ValueError:
if schema is None:
raise
elif not isinstance(schema, XMLSchemaBase):
schema = cls(schema, validation='strict', locations=locations, base_url=base_url,
defuse=defuse, timeout=timeout)
else:
schema = cls(schema, validation='strict', locations=locations, defuse=defuse, timeout=timeout)
elif not isinstance(schema, XMLSchemaBase):
schema = cls(schema, validation='strict', locations=locations, base_url=base_url,
defuse=defuse, timeout=timeout)
if not isinstance(source, XMLResource):
source = XMLResource(source, defuse=defuse, timeout=timeout, lazy=lazy)

View File

@ -557,7 +557,7 @@ class XsdElement(XsdComponent, ValidationMixin, ParticleMixin, ElementPathMixin)
if 'filler' in kwargs:
value = kwargs['filler'](self)
else:
if level == 0:
if level == 0 or self.xsd_version != '1.0':
kwargs['_skip_id'] = True
for result in xsd_type.iter_decode(text, validation, **kwargs):
if isinstance(result, XMLSchemaValidationError):

View File

@ -516,7 +516,7 @@ class XsdGroup(XsdComponent, ModelGroup, ValidationMixin):
if xsd_type.is_derived(model_element.type, derivation):
reason = "usage of %r with type %s is blocked by head element"
raise XMLSchemaValidationError(self, reason % (xsd_element, derivation))
if XSI_TYPE not in elem.attrib:
return

View File

@ -1259,6 +1259,8 @@ class XMLSchemaBase(XsdValidator, ValidationMixin, ElementPathMixin):
converter = self.get_converter(converter, namespaces, **kwargs)
id_map = Counter()
inherited = {}
if decimal_type is not None:
kwargs['decimal_type'] = decimal_type
if filler is not None:
@ -1272,7 +1274,7 @@ class XMLSchemaBase(XsdValidator, ValidationMixin, ElementPathMixin):
for obj in xsd_element.iter_decode(
elem, validation, converter=converter, source=source, namespaces=namespaces,
use_defaults=use_defaults, datetime_types=datetime_types,
fill_missing=fill_missing, id_map=id_map, **kwargs):
fill_missing=fill_missing, id_map=id_map, inherited=inherited, **kwargs):
yield obj
for k, v in id_map.items():

View File

@ -516,7 +516,7 @@ class XsdAtomicBuiltin(XsdAtomic):
yield self.decode_error(validation, obj, self.to_python,
reason="value is not an instance of {!r}".format(self.instance_types))
if self.name == XSD_ID and '_skip_id' not in kwargs:
if self.name == XSD_ID:
try:
id_map = kwargs['id_map']
except KeyError:
@ -527,7 +527,7 @@ class XsdAtomicBuiltin(XsdAtomic):
except TypeError:
id_map[obj] = 1
if id_map[obj] > 1:
if id_map[obj] > 1 and '_skip_id' not in kwargs:
yield self.validation_error(validation, "Duplicated xsd:ID value {!r}".format(obj))
elif self.name == XSD_IDREF: