'How to use root.findall with AND condition

XML be something like this:

<Section SectionLevel="1" SectionID="Food">
    <Section SectionLevel="2" SectionID="Fruit">
        <Content>Apple</Content>
</Section>
        
<Section SectionLevel="1" SectionID="Food">
    <Section SectionLevel="2" SectionID="Meat">
        <Content>Beef</Content>
</Section>

I want to print content when SectionID in level 1 = Food and SectionID in level 2 = Fruit. I couldn't find tutorial using XPath with AND condition.

def main():
    tree = ET.parse('data.xml')
    root = tree.getroot()
    for section in root.findall("./Section/[SectionID='Food']"):

    and

    for section in root.findall("./Section/Section/[SectionID='Fruit']"):

    print(content)


Solution 1:[1]

There is and in xpath, but I don't think you need it in this case. I think you just need to put your two xpath's together...

.//Section[@SectionID='Food']/Section[@SectionID='Fruit']/Content

Full example...

import xml.etree.ElementTree as ET
from io import StringIO  # using StringIO to simulate reading a file instead of a string

xml = """<doc>
<Section SectionLevel="1" SectionID="Food">
    <Section SectionLevel="2" SectionID="Fruit">
        <Content>Apple</Content>
    </Section>
</Section>
        
<Section SectionLevel="1" SectionID="Food">
    <Section SectionLevel="2" SectionID="Meat">
        <Content>Beef</Content>
    </Section>
</Section>
</doc>"""

doc = StringIO(xml)
tree = ET.parse(doc)

for content_elem in tree.findall(".//Section[@SectionID='Food']/Section[@SectionID='Fruit']/Content"):
    print(content_elem.text)

Printed output...

Apple

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Haley