Here's something I don't get and hope someone can clarify for me.
With a simple XML tree, it's easy enough to get an attribute value from the first instance of a given element.
In [1]: import xml.etree.ElementTree as ET
In [2]: xml = """<?xml version="1.0" standalone="no"?>
...: <root>
...: <level1>
...: <level2
...: id='abc'
...: name='123'
...: />
...: </level1>
...: </root>"""
In [3]: root = ET.fromstring(xml)
In [4]: root.find('./level1/level2').attrib['id']
Out[4]: 'abc'
Now, if I add a namespace to the root, it doesn't work anymore, even with what I understand in the correct syntax:
In [6]: xml = """<?xml version="1.0" standalone="no"?>
...: <root xmlns="http://namespace.com">
...: <level1>
...: <level2
...: id='abc'
...: name='123'
...: />
...: </level1>
...: </root>"""
In [7]: root = ET.fromstring(xml)
In [8]: root.find('{http://namespace.com}./level1/level2').attrib['id']
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-2389d900412a> in <module>
----> 1 root.find('{http://namespace.com}./level1/level2').attrib['id']
AttributeError: 'NoneType' object has no attribute 'attrib'
Why is that, and what should I do instead?