Home > Back-end >  Python ElementTree - Select a nested xml element based on the value of an attribute
Python ElementTree - Select a nested xml element based on the value of an attribute

Time:07-28

I'm trying to select a nested element based on the value of an attribute

from xml.etree import ElementTree as ET

xml = '''<?xml version="1.0" encoding="UTF-8"?>
    <Report>
        <h1>
            <h2>
                <Element name='Bob' age='32' title='President'></Element>
                <Element name='Sue' age='25' title='Vice-President'></Element>
                <Element name='Mary' age='44' title='Secretary'></Element>
            </h2>
        </h1>
    </Report>'''

root = ET.fromstring(xml)

element = root.findall("//*[@title='President']")

But I get the following error for the element variable assignment

SyntaxError: cannot use absolute path on element

How can I pull the attribute data for the President element only?

CodePudding user response:

You were very close :-)

The below works - look at the dot in the xpath..

xml = '''<?xml version="1.0" encoding="UTF-8"?>
    <Report>
        <h1>
            <h2>
                <Element name='Bob' age='32' title='President'></Element>
                <Element name='Sue' age='25' title='Vice-President'></Element>
                <Element name='Mary' age='44' title='Secretary'></Element>
            </h2>
        </h1>
    </Report>'''

root = ET.fromstring(xml)
elements = root.findall(".//*[@title='President']")
print(elements)

CodePudding user response:

is this what you're looking for?

from xml.etree import ElementTree as ET

xml = '''<?xml version="1.0" encoding="UTF-8"?>
    <Report>
        <h1>
            <h2>
                <Element name='Bob' age='32' title='President'></Element>
                <Element name='Sue' age='25' title='Vice-President'></Element>
                <Element name='Mary' age='44' title='Secretary'></Element>
            </h2>
        </h1>
    </Report>'''


root = ET.fromstring(xml)
elements = root.findall(".//*[@title='President']")
for i in elements:
    print(i.attrib['name'], i.attrib['age'], i.attrib['title'])
  • Related