codecompl...@free.fr schrieb am 04.09.21 um 14:09:
Here's some code I found to strip namespaces after parsing, without relying on
a regex:
```
# Remove namespace prefixes
#Source: https://stackoverflow.com/questions/60486563/
tree = et.parse(INPUTFILE)
root = tree.getroot()
for elem in root.getiterator():
Use root.iter('*') or root.iter(et.Element) instead to select only elements.
#ValueError: Invalid input tag of type <class
'cython_function_or_method'>
#et.tag = et.QName(elem).localname
# For elements, replace qualified name with localname
if not(type(elem) == et._Comment):
There is an iselement() function for this purpose, but you don't need that
at all if you choose to iterate only over elements (which you should).
elem.tag = et.QName(elem).localname
# Remove attributes that are in a namespace
for attr in elem.attrib:
if "{" in attr:
elem.attrib.pop(attr)
# Remove unused namespace declarations
et.cleanup_namespaces(root)
```
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com