How to iterate over XML children with same name as current element and avoid current element in iteration?
Question:
I have
A given XML (can’t change naming) that have same name for a node and its direct children, here items
I want
To iterate on the children only, the items
that have a description
field
My issue
The parent node of type items
appears in the iteration, even the iter
is called on itself if I understand well
from xml.etree import ElementTree
content = """<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<items>
<description>foo1</description>
</items>
<items>
<description>foo2</description>
</items>
</items>
</root>
"""
tree = ElementTree.fromstring(content)
print(">>", tree.find("items"))
for item in tree.find("items").iter("items"):
print(item, item.find("description"))
Current output
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8720> None
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
Expected output
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
Answers:
Use XPath with findall().
tree.findall('items/items')
I have
A given XML (can’t change naming) that have same name for a node and its direct children, here items
I want
To iterate on the children only, the items
that have a description
field
My issue
The parent node of type items
appears in the iteration, even the iter
is called on itself if I understand well
from xml.etree import ElementTree
content = """<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<items>
<description>foo1</description>
</items>
<items>
<description>foo2</description>
</items>
</items>
</root>
"""
tree = ElementTree.fromstring(content)
print(">>", tree.find("items"))
for item in tree.find("items").iter("items"):
print(item, item.find("description"))
Current output
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8720> None
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
Expected output
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
Use XPath with findall().
tree.findall('items/items')