我有以下代码片段:
from xml.etree.ElementTree import fromstring,tostring
mathml = fromstring(input)
for elem in mathml.getiterator():
elem.tag = 'm:' + elem.tag
return tostring(mathml)
Run Code Online (Sandbox Code Playgroud)
当我输入以下内容时input:
<math>
<a> 1 2 3 </a> <b />
<foo>Uitleg</foo>
<!-- <bar> -->
</math>
Run Code Online (Sandbox Code Playgroud)
它导致:
<m:math>
<m:a> 1 2 3 </m:a> <m:b />
<m:foo>Uitleg</m:foo>
</m:math>
Run Code Online (Sandbox Code Playgroud)
怎么会?我该如何保留评论?
编辑:我不关心使用的确切xml库,但是,我应该能够对标记进行粘贴更改.不幸的是,lxml似乎不允许这样(我不能使用正确的命名空间操作)