Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support defaultdict for namespace mapping #211

Merged
merged 1 commit into from
Mar 28, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_xmltodict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from xmltodict import parse, ParsingInterrupted
import collections
import unittest

try:
Expand Down Expand Up @@ -227,6 +228,35 @@ def test_namespace_collapse(self):
res = parse(xml, process_namespaces=True, namespaces=namespaces)
self.assertEqual(res, d)

def test_namespace_collapse_all(self):
xml = """
<root xmlns="http://defaultns.com/"
xmlns:a="http://a.com/"
xmlns:b="http://b.com/">
<x a:attr="val">1</x>
<a:y>2</a:y>
<b:z>3</b:z>
</root>
"""
namespaces = collections.defaultdict(lambda: None)
d = {
'root': {
'x': {
'@xmlns': {
'': 'http://defaultns.com/',
'a': 'http://a.com/',
'b': 'http://b.com/',
},
'@attr': 'val',
'#text': '1',
},
'y': '2',
'z': '3',
},
}
res = parse(xml, process_namespaces=True, namespaces=namespaces)
self.assertEqual(res, d)

def test_namespace_ignore(self):
xml = """
<root xmlns="http://defaultns.com/"
Expand Down
7 changes: 5 additions & 2 deletions xmltodict.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ def __init__(self,
self.force_list = force_list

def _build_name(self, full_name):
if not self.namespaces:
if self.namespaces is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although a defaultdict essentially has infinite items, until an item is actually inserted (either explicitly or on first access), it has zero length and evaluates to False.

return full_name
i = full_name.rfind(self.namespace_separator)
if i == -1:
return full_name
namespace, name = full_name[:i], full_name[i+1:]
short_namespace = self.namespaces.get(namespace, namespace)
try:
short_namespace = self.namespaces[namespace]
except KeyError:
short_namespace = namespace
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the documentation on defaultdict:

Note that __missing__() is not called for any operations besides __getitem__(). This means that get() will, like normal dictionaries, return None as a default rather than using default_factory.

While a bit more verbose, this change uses __getitem__() instead of get(), so the defaultdict behaves as expected.

if not short_namespace:
return name
else:
Expand Down