Cómo evitar que xml.ElementTree de la cadena deje caer commentnode
Tengo el siguiente fragmento de código:
from xml.etree.ElementTree import fromstring,tostring
mathml = fromstring(input)
for elem in mathml.getiterator():
elem.tag = 'm:' + elem.tag
return tostring(mathml)
Cuando ingreso el siguienteinput
:
<math>
<a> 1 2 3 </a> <b />
<foo>Uitleg</foo>
<!-- <bar> -->
</math>
En resultado de
<m:math>
<m:a> 1 2 3 </m:a> <m:b />
<m:foo>Uitleg</m:foo>
</m:math>
¿Cómo? ¿Y cómo puedo preservar el comentario?
edita: No me importa la biblioteca xml exacta utilizada, sin embargo, debería poder hacer el cambio pegado a las etiquetas. Desafortunadamente, lxml no parece permitir esto (y no puedo usar las operaciones de espacio de nombres adecuadas)