Fix etree_to_string to avoid registering reserved prefixes
This commit is contained in:
parent
d89a597c82
commit
22fdcc9a5a
|
@ -153,19 +153,21 @@ def etree_tostring(elem, namespaces=None, indent='', max_lines=None, spaces_for_
|
|||
if isinstance(elem, etree_element):
|
||||
if namespaces:
|
||||
for prefix, uri in namespaces.items():
|
||||
etree_register_namespace(prefix, uri)
|
||||
if not re.match(r'ns\d+$', prefix):
|
||||
etree_register_namespace(prefix, uri)
|
||||
tostring = ElementTree.tostring
|
||||
|
||||
elif isinstance(elem, py_etree_element):
|
||||
if namespaces:
|
||||
for prefix, uri in namespaces.items():
|
||||
PyElementTree.register_namespace(prefix, uri)
|
||||
if not re.match(r'ns\d+$', prefix):
|
||||
PyElementTree.register_namespace(prefix, uri)
|
||||
tostring = PyElementTree.tostring
|
||||
|
||||
elif lxml_etree is not None:
|
||||
if namespaces:
|
||||
for prefix, uri in namespaces.items():
|
||||
if prefix:
|
||||
if prefix and not re.match(r'ns\d+$', prefix):
|
||||
lxml_etree_register_namespace(prefix, uri)
|
||||
tostring = lxml_etree.tostring
|
||||
else:
|
||||
|
|
|
@ -224,7 +224,7 @@ class XMLSchemaValidationError(XMLSchemaValidatorError, ValueError):
|
|||
msg.append('Reason: %s\n' % self.reason)
|
||||
if hasattr(self.validator, 'tostring'):
|
||||
msg.append("Schema:\n\n%s\n" % self.validator.tostring(' ', 20))
|
||||
if self.elem is not None:
|
||||
if is_etree_element(self.elem):
|
||||
elem_as_string = etree_tostring(self.elem, self.namespaces, ' ', 20)
|
||||
if hasattr(self.elem, 'sourceline'):
|
||||
msg.append("Instance (line %r):\n\n%s\n" % (self.elem.sourceline, elem_as_string))
|
||||
|
|
Loading…
Reference in New Issue