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>
Asked By: azro

||

Answers:

Use XPath with findall().

tree.findall('items/items')
Answered By: Michael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.