Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do not apply default namespace to attribute names
Unprefixed attributes are considered to not be in any namespace,
according to the XML namespaces spec.

This also fixes issue 17088, since the exception that rejects
non-qualified names when using the default_namespace option is
no longer raised when serializing an attribute name.
  • Loading branch information
mthuurne committed Dec 9, 2018
commit d80197ecd9c2e0f9bdecdf07e1e83689d5aab717
12 changes: 6 additions & 6 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,11 @@ def _namespaces(elem, default_namespace=None):
# maps uri:s to prefixes
namespaces = {}

def serialize_qname(qname):
def serialize_qname(qname, is_attr):
# calculate serialized qname representation
if qname[:1] == "{":
uri, local = qname[1:].rsplit("}", 1)
if uri == default_namespace:
if not is_attr and uri == default_namespace:
prefix = ""
else:
prefix = namespaces.get(uri)
Expand All @@ -863,10 +863,10 @@ def serialize_qname(qname):
else:
return local # default element
else:
if default_namespace:
if not is_attr and default_namespace:
# FIXME: can this be handled in XML 1.0?
raise ValueError(
"cannot use non-qualified name (%s) with "
"cannot use non-qualified name (<%s>) with "
"default_namespace option" % qname
)
return qname
Expand All @@ -875,11 +875,11 @@ def add_qname(qname, is_attr=False):
ser_tag, ser_attr = qnames.get(qname, (None, None))
if is_attr:
if ser_attr is None:
ser_attr = serialize_qname(qname)
ser_attr = serialize_qname(qname, True)
qnames[qname] = (ser_tag, ser_attr)
else:
if ser_tag is None:
ser_tag = serialize_qname(qname)
ser_tag = serialize_qname(qname, False)
qnames[qname] = (ser_tag, ser_attr)

# populate qname and namespaces table
Expand Down