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

Allow multiple XML-tags with the same name to be parsed as list #422

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions feedparser/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,18 @@ def pop(self, element, strip_whitespace=1):
old_value_depth = self.property_depth_map.setdefault(
self.entries[-1], {}
).get(element)
if old_value_depth is None or self.depth <= old_value_depth:
self.property_depth_map[self.entries[-1]][element] = self.depth
self.entries[-1][element] = output
self.property_depth_map[self.entries[-1]][element] = self.depth
# Legacy elements are squashed to a singel item
legacy_elements = ["title", "summary", "author", "id"]
if element in self.entries[-1] and element not in legacy_elements:
previous_output = self.entries[-1][element]
if not isinstance(previous_output, list):
previous_output = [previous_output]
previous_output.append(output)
self.entries[-1][element] = previous_output
else:
if old_value_depth is None or self.depth <= old_value_depth:
self.entries[-1][element] = output
if self.incontent:
contentparams = copy.deepcopy(self.contentparams)
contentparams["value"] = output
Expand Down
10 changes: 10 additions & 0 deletions tests/wellformed/namespace/multiple-elements-with-same-key.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
Description: Ensure multiple elements are parsed into a list
Expect: not bozo and entries[0]['madeup_element'] == ['foo', 'bar']
-->
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:MadeUp="http://madeup.test">
<item>
<MadeUp:Element>foo</MadeUp:Element>
<MadeUp:Element>bar</MadeUp:Element>
</item>
</feed>
8 changes: 4 additions & 4 deletions tests/wellformed/node_precedence/atom10_arbitrary_element.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<!--
Description: The arbitrarily named node of the least depth should be preferred to those with the same name of greater depth.
Expect: not bozo and entries[0]['abcdefg'] == entries[1]['abcdefg'] == 'Correct Value'
Expect: not bozo and entries[0]['abcdefg'] == ['Correct Value', 'Incorrect Value'] and entries[1]['abcdefg'] == ['Incorrect Value', 'Correct Value']
-->
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<abcdefg>Correct Value</abcdefg>
<image>
<abcdefg>Incorrect Value</abcdefg>
<abcdefg>Incorrect Value</abcdefg>
</image>
</entry>
<entry>
<image>
<abcdefg>Incorrect Value</abcdefg>
<abcdefg>Incorrect Value</abcdefg>
</image>
<abcdefg>Correct Value</abcdefg>
</entry>
</feed>
</feed>