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

Switch namespace separator from colon to pipe (fixes #289) #290

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ By default, `xmltodict` does no XML namespace processing (it just treats namespa
... </root>
... """
>>> xmltodict.parse(xml, process_namespaces=True) == {
... 'http://defaultns.com/:root': {
... 'http://defaultns.com/:x': '1',
... 'http://a.com/:y': '2',
... 'http://b.com/:z': '3',
... 'http://defaultns.com/|root': {
... 'http://defaultns.com/|x': '1',
... 'http://a.com/|y': '2',
... 'http://b.com/|z': '3',
... }
... }
True
Expand All @@ -67,8 +67,8 @@ It also lets you collapse certain namespaces to shorthand prefixes, or skip them
>>> xmltodict.parse(xml, process_namespaces=True, namespaces=namespaces) == {
... 'root': {
... 'x': '1',
... 'ns_a:y': '2',
... 'http://b.com/:z': '3',
... 'ns_a|y': '2',
... 'http://b.com/|z': '3',
... },
... }
True
Expand Down
10 changes: 5 additions & 5 deletions tests/test_dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ def test_short_empty_elements(self):

def test_namespace_support(self):
obj = OrderedDict((
('http://defaultns.com/:root', OrderedDict((
('http://defaultns.com/|root', OrderedDict((
('@xmlns', OrderedDict((
('', 'http://defaultns.com/'),
('a', 'http://a.com/'),
('b', 'http://b.com/'),
))),
('http://defaultns.com/:x', OrderedDict((
('@http://a.com/:attr', 'val'),
('http://defaultns.com/|x', OrderedDict((
('@http://a.com/|attr', 'val'),
('#text', '1'),
))),
('http://a.com/:y', '2'),
('http://b.com/:z', '3'),
('http://a.com/|y', '2'),
('http://b.com/|z', '3'),
))),
))
ns = {
Expand Down
16 changes: 8 additions & 8 deletions tests/test_xmltodict.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,18 @@ def test_namespace_support(self):
</root>
"""
d = {
'http://defaultns.com/:root': {
'http://defaultns.com/:x': {
'http://defaultns.com/|root': {
'http://defaultns.com/|x': {
'@xmlns': {
'': 'http://defaultns.com/',
'a': 'http://a.com/',
'b': 'http://b.com/',
},
'@http://a.com/:attr': 'val',
'@http://a.com/|attr': 'val',
'#text': '1',
},
'http://a.com/:y': '2',
'http://b.com/:z': '3',
'http://a.com/|y': '2',
'http://b.com/|z': '3',
}
}
res = parse(xml, process_namespaces=True)
Expand Down Expand Up @@ -229,11 +229,11 @@ def test_namespace_collapse(self):
'a': 'http://a.com/',
'b': 'http://b.com/',
},
'@ns_a:attr': 'val',
'@ns_a|attr': 'val',
'#text': '1',
},
'ns_a:y': '2',
'http://b.com/:z': '3',
'ns_a|y': '2',
'http://b.com/|z': '3',
},
}
res = parse(xml, process_namespaces=True, namespaces=namespaces)
Expand Down
10 changes: 5 additions & 5 deletions xmltodict.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self,
postprocessor=None,
dict_constructor=OrderedDict,
strip_whitespace=True,
namespace_separator=':',
namespace_separator='|',
namespaces=None,
force_list=None,
comment_key='#comment'):
Expand Down Expand Up @@ -196,7 +196,7 @@ def _should_force_list(self, key, value):


def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
namespace_separator=':', disable_entities=True, process_comments=False, **kwargs):
namespace_separator='|', disable_entities=True, process_comments=False, **kwargs):
"""Parse the given XML input and convert it into a dictionary.

`xml_input` can either be a `string`, a file-like object, or a generator of strings.
Expand Down Expand Up @@ -375,7 +375,7 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
return handler.item


def _process_namespace(name, namespaces, ns_sep=':', attr_prefix='@'):
def _process_namespace(name, namespaces, ns_sep='|', attr_prefix='@'):
if not namespaces:
return name
try:
Expand All @@ -386,7 +386,7 @@ def _process_namespace(name, namespaces, ns_sep=':', attr_prefix='@'):
ns_res = namespaces.get(ns.strip(attr_prefix))
name = '{}{}{}{}'.format(
attr_prefix if ns.startswith(attr_prefix) else '',
ns_res, ns_sep, name) if ns_res else name
ns_res, ':', name) if ns_res else name
return name


Expand All @@ -398,7 +398,7 @@ def _emit(key, value, content_handler,
pretty=False,
newl='\n',
indent='\t',
namespace_separator=':',
namespace_separator='|',
namespaces=None,
full_document=True,
expand_iter=None):
Expand Down