Sometimes, we want to ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module.
In this article, we’ll look at how to ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module.
How to ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module?
To ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module, we can strip the namespace from the DOM object with the rpartition method.
For instance, we write
from io import StringIO
import xml.etree.ElementTree as ET
# ...
it = ET.iterparse(StringIO(xml))
for _, el in it:
prefix, has_namespace, postfix = el.tag.partition('}')
if has_namespace:
el.tag = postfix
root = it.root
to call ET.iterparse to parse rhe xml file.
Then we loop through the nodes with a for loop.
In it, we call el.tag.rpartition with '}' to get the tag value from postfix.
Then we check if has_namespace is True.
If it is, then we assign el.tag to postfix.
Conclusion
To ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module, we can strip the namespace from the DOM object with the rpartition method.