[英]Python: How to add a prefix to tags in an xml.etree.ElementTree
I use the python asciimathml library to parse some asciimathml and convert it to MathML 我使用python asciimathml库来解析一些asciimathml并将其转换为MathML
>>> from xml.etree.ElementTree import tostring
>>> tostring(asciimathml.parse('sqrt 2'))
'<math><mstyle><msqrt><mn>2</mn></msqrt></mstyle></math>'
The only trouble is I need my tags with a m:
prefix. 唯一的麻烦是我需要带有
m:
前缀的标签。 How do I change above code so I get: 我如何更改上面的代码,所以我得到:
'<m:math><m:mstyle><m:msqrt><m:mn>2</m:mn></m:msqrt></m:mstyle></m:math>'
You can rename the tag, adding the 'm:' prefix: 您可以重命名标签,并添加“ m:”前缀:
import asciimathml
from xml.etree.ElementTree import tostring
tree = asciimathml.parse('sqrt 2')
for elem in tree.getiterator():
elem.tag = 'm:' + elem.tag
print tostring(tree)
Result: 结果:
<m:math><m:mstyle><m:msqrt><m:mn>2</m:mn></m:msqrt></m:mstyle></m:math>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.