all Python 3xml.etree.ElementTreefunction
xml.etree.ElementTree.XMLID()
parsing
Parses XML from a string and returns a pair: the root and a dict mapping id attribute values to elements.
Syntax
root, ids = ET.XMLID(text, parser=None)
Only an attribute literally named id counts; xml:id or ID are ignored. With duplicate ids the dict silently keeps the last element. It returns a tuple, so root = ET.XMLID(s) gives a tuple, not an element; unpack it.
Examples
import xml.etree.ElementTree as ET
root, ids = ET.XMLID('<a><b id="x">1</b><c id="x">2</c><d ID="y"/></a>')
print(list(ids), ids['x'].tag) # → ['x'] c
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy