Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3701,6 +3701,14 @@ def test_simple_roundtrip(self):
#self.assertEqual(c14n_roundtrip("<doc xmlns:x='http://example.com/x' xmlns='http://example.com/default'><b y:a1='1' xmlns='http://example.com/default' a3='3' xmlns:y='http://example.com/y' y:a2='2'/></doc>"),
#'<doc xmlns:x="http://example.com/x"><b xmlns:y="http://example.com/y" a3="3" y:a1="1" y:a2="2"></b></doc>')

# Namespace issues
xml = '<X xmlns="http://nps/a"><Y targets="abc,xyz"></Y></X>'
self.assertEqual(c14n_roundtrip(xml), xml)
xml = '<X xmlns="http://nps/a"><Y xmlns="http://nsp/b" targets="abc,xyz"></Y></X>'
self.assertEqual(c14n_roundtrip(xml), xml)
xml = '<X xmlns="http://nps/a"><Y xmlns:b="http://nsp/b" b:targets="abc,xyz"></Y></X>'
self.assertEqual(c14n_roundtrip(xml), xml)

def test_c14n_exclusion(self):
xml = textwrap.dedent("""\
<root xmlns:x="http://example.com/x">
Expand Down
5 changes: 5 additions & 0 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,11 @@ def _qname(self, qname, uri=None):
self._declared_ns_stack[-1].append((uri, prefix))
return f'{prefix}:{tag}' if prefix else tag, tag, uri

if not uri:
# As soon as a default namespace is defined,
# anything that has no namespace (and thus, no prefix) goes there.
return tag, tag, uri

raise ValueError(f'Namespace "{uri}" is not declared in scope')

def data(self, data):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
C14N 2.0 serialisation in xml.etree.ElementTree failed for unprefixed attributes
when a default namespace was defined.