Whole file
martinblech/xmltodict
The author described this change as “fix: preserve namespace declaration when parsing”. It counts as a record because the checks below fail on the code as it stood at 05c171a9c and pass on 61047021c, with nothing else changed between the two runs.
Projectmartinblech/xmltodict
Fix saved2016-06-05
Sharing licenceMIT · LICENSE
Change size+8 −0
What the code was meant to do, written into the code itself as a save note
fix: preserve namespace declaration when parsing
The change
| 71 | 71 | self.strip_whitespace = strip_whitespace | |
| 72 | 72 | self.namespace_separator = namespace_separator | |
| 73 | 73 | self.namespaces = namespaces | |
| 74 | + | self.namespace_declarations = OrderedDict() | |
| 74 | 75 | self.force_list = force_list | |
| 75 | 76 | ||
| 76 | 77 | def _build_name(self, full_name): | |
| ⋯ | |||
| 91 | 92 | return attrs | |
| 92 | 93 | return self.dict_constructor(zip(attrs[0::2], attrs[1::2])) | |
| 93 | 94 | ||
| 95 | + | def startNamespaceDecl(self, prefix, uri): | |
| 96 | + | self.namespace_declarations[prefix or ''] = uri | |
| 97 | + | ||
| 94 | 98 | def startElement(self, full_name, attrs): | |
| 95 | 99 | name = self._build_name(full_name) | |
| 96 | 100 | attrs = self._attrs_to_dict(attrs) | |
| 101 | + | if attrs and self.namespace_declarations: | |
| 102 | + | attrs['xmlns'] = self.namespace_declarations | |
| 103 | + | self.namespace_declarations = OrderedDict() | |
| 97 | 104 | self.path.append((name, attrs or None)) | |
| 98 | 105 | if len(self.path) > self.item_depth: | |
| 99 | 106 | self.stack.append((self.item, self.data)) | |
| ⋯ | |||
| 301 | 308 | except AttributeError: | |
| 302 | 309 | # Jython's expat does not support ordered_attributes | |
| 303 | 310 | pass | |
| 311 | + | parser.StartNamespaceDeclHandler = handler.startNamespaceDecl | |
| 304 | 312 | parser.StartElementHandler = handler.startElement | |
| 305 | 313 | parser.EndElementHandler = handler.endElement | |
| 306 | 314 | parser.CharacterDataHandler = handler.characters | |
The check that tells the two apart
fail→pass·tests/test_xmltodict.py::XMLToDictTestCase::test_namespace_collapse
fail→pass·tests/test_xmltodict.py::XMLToDictTestCase::test_namespace_support
Check file tests/test_xmltodict.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it05c171a9ce2406dd914364cdf7a497f4eeb62239
Broken version dated2016-04-05
Modulexmltodict
Units changed_DictSAXHandler, parse
Fingerprint24e5cf3109636495
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in martinblech/xmltodict
- 2026-02-15fix(unparse): serialize None text/attrs as empty values (fixes #401)
- 2025-09-17fix: allow DOCTYPE with disable_entities=True (default)
- 2025-09-16fix: fail closed when entities disabled
- 2025-09-16fix: validate XML comments
- 2025-09-12fix(unparse): handle non-string `#text` with attributes; unify value conversion
- 2025-09-12fix(unparse): skip empty lists to keep pretty/compact outputs consistent