Another option would be to iterate over the attributes and return the value of the attribute with a local-name that ends in backgroundImage
.
Example...
from xml.etree import ElementTree as ET
XML = '''
<body xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
xmlns:smpte="http://smpte-ra.org/schemas/2052-1/2013/smpte-tt">
<div region="imageRegion" xml:id="img_SUB6756004155_0"
ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0"></div>
</body>'''
root = ET.fromstring(XML)
div = root.find("div")
val = next((v for k, v in div.attrib.items() if k.endswith('backgroundImage')), None)
if val:
print(f"Value: {val}")
Outputs...
Value: #SUB6756004155_0
This can be fragile though. It only returns the first attribute found.
If that's a problem, maybe use a list instead:
val = [v for k, v in div.attrib.items() if k.endswith('backgroundImage')]
It would also incorrectly return an attribute that ends with "backgroundImage" (like "invalid_backgroundImage").
If that's a problem, maybe use regex instead:
val = next((v for k, v in div.attrib.items() if re.match(r".*}backgroundImage$", "}" + k)), None)
If you're ever able to switch to lxml, the testing of the local-name can be done in xpath...
val = div.xpath("@*[local-name()='backgroundImage']")