Skip to content

Commit 2c0dfd0

Browse files
committed
Allow multiple XML-tags with the same name to be parsed as list
1 parent 0af72dc commit 2c0dfd0

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

feedparser/mixin.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,18 @@ def pop(self, element, strip_whitespace=1):
635635
old_value_depth = self.property_depth_map.setdefault(
636636
self.entries[-1], {}
637637
).get(element)
638-
if old_value_depth is None or self.depth <= old_value_depth:
639-
self.property_depth_map[self.entries[-1]][element] = self.depth
640-
self.entries[-1][element] = output
638+
self.property_depth_map[self.entries[-1]][element] = self.depth
639+
# Legacy elements are squashed to a singel item
640+
legacy_elements = ["title", "summary", "author", "id"]
641+
if element in self.entries[-1] and element not in legacy_elements:
642+
previous_output = self.entries[-1][element]
643+
if not isinstance(previous_output, list):
644+
previous_output = [previous_output]
645+
previous_output.append(output)
646+
self.entries[-1][element] = previous_output
647+
else:
648+
if old_value_depth is None or self.depth <= old_value_depth:
649+
self.entries[-1][element] = output
641650
if self.incontent:
642651
contentparams = copy.deepcopy(self.contentparams)
643652
contentparams["value"] = output
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
Description: Ensure multiple elements are parsed into a list
3+
Expect: not bozo and entries[0]['madeup_element'] == ['foo', 'bar']
4+
-->
5+
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:MadeUp="http://madeup.test">
6+
<item>
7+
<MadeUp:Element>foo</MadeUp:Element>
8+
<MadeUp:Element>bar</MadeUp:Element>
9+
</item>
10+
</feed>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<!--
22
Description: The arbitrarily named node of the least depth should be preferred to those with the same name of greater depth.
3-
Expect: not bozo and entries[0]['abcdefg'] == entries[1]['abcdefg'] == 'Correct Value'
3+
Expect: not bozo and entries[0]['abcdefg'] == ['Correct Value', 'Incorrect Value'] and entries[1]['abcdefg'] == ['Incorrect Value', 'Correct Value']
44
-->
55
<feed xmlns="http://www.w3.org/2005/Atom">
66
<entry>
77
<abcdefg>Correct Value</abcdefg>
88
<image>
9-
<abcdefg>Incorrect Value</abcdefg>
9+
<abcdefg>Incorrect Value</abcdefg>
1010
</image>
1111
</entry>
1212
<entry>
1313
<image>
14-
<abcdefg>Incorrect Value</abcdefg>
14+
<abcdefg>Incorrect Value</abcdefg>
1515
</image>
1616
<abcdefg>Correct Value</abcdefg>
1717
</entry>
18-
</feed>
18+
</feed>

0 commit comments

Comments
 (0)