Skip to content

Commit

Permalink
Allow multiple XML-tags with the same name to be parsed as list
Browse files Browse the repository at this point in the history
  • Loading branch information
fliiiix committed Dec 29, 2023
1 parent 0af72dc commit e4577ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
13 changes: 11 additions & 2 deletions feedparser/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,17 @@ 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.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)
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>

0 comments on commit e4577ab

Please sign in to comment.