Split tag and attrib only once in tostring().

This commit is contained in:
Emmanuel Gil Peyrot 2019-01-09 14:55:27 +01:00
parent 5f25b0b6a0
commit f7e4caadfe

View file

@ -45,11 +45,12 @@ def tostring(xml=None, xmlns='', stream=None, outbuffer='',
output = [outbuffer] output = [outbuffer]
# Extract the element's tag name. # Extract the element's tag name.
tag_name = xml.tag.split('}', 1)[-1] tag_split = xml.tag.split('}', 1)
tag_name = tag_split[-1]
# Extract the element's namespace if it is defined. # Extract the element's namespace if it is defined.
if '}' in xml.tag: if '}' in xml.tag:
tag_xmlns = xml.tag.split('}', 1)[0][1:] tag_xmlns = tag_split[0][1:]
else: else:
tag_xmlns = '' tag_xmlns = ''
@ -82,8 +83,9 @@ def tostring(xml=None, xmlns='', stream=None, outbuffer='',
if '}' not in attrib: if '}' not in attrib:
output.append(' %s="%s"' % (attrib, value)) output.append(' %s="%s"' % (attrib, value))
else: else:
attrib_ns = attrib.split('}')[0][1:] attrib_split = attrib.split('}')
attrib = attrib.split('}')[1] attrib_ns = attrib_split[0][1:]
attrib = attrib_split[1]
if attrib_ns == XML_NS: if attrib_ns == XML_NS:
output.append(' xml:%s="%s"' % (attrib, value)) output.append(' xml:%s="%s"' % (attrib, value))
elif stream and attrib_ns in stream.namespace_map: elif stream and attrib_ns in stream.namespace_map: