Is there a way to ignore the XML namespace in tage names in elementtree.ElementTree?
I try to print all technicalContact tags:
for item in root.getiterator(tag='{http://www.example.com}technicalContact'):
print item.tag, item.text
And I get something like:
{http://www.example.com}technicalContact blah@example.com
But what I really want is:
technicalContact blah@example.com
Is there a way to display only the suffix (sans xmlns), or better – iterate over the elements without explicitly stating xmlns?
You can define a generator to recursively search through your element tree in order to find tags which end with the appropriate tag name. For example, something like this:
This just checks for tags which end with
tag, i.e. ignoring any leading namespace. You can then iterate over any tag you want as follows:This generator in action: