【发布时间】:2013-09-25 07:04:28
【问题描述】:
短版: 如何使用 lxml 将 xmlns:xi="http://www.w3.org/2001/XInclude" 前缀声明添加到我在 python 中的根元素中?
上下文:
我有一些 XML 文件,其中包含其他文件的 ID。
这些 ID 代表引用的文件名。
使用 lxml 我设法将这些替换为适当的 XInclude 语句,但如果我没有前缀声明,我的 XML 解析器将不会添加包含,这是正常的。
编辑: 我不会包含我的代码,因为它无助于理解问题。我可以很好地处理文档,我的问题是序列化。
所以从这个
<root>
<somechild/>
</root>
我想在我的输出文件中得到这个<root xmlns:xi="http://www.w3.org/2001/XInclude">
<somechild/>
</root>。
为此,我尝试使用
`
tree = ET.parse(fileName)
root = tree.getroot()
root.nsmap['xi'] = "http://www.w3.org/2001/XInclude"
tree.write('output.xml', xml_declaration=True, encoding="UTF-8", pretty_print=True)
`
【问题讨论】:
标签: python xml namespaces lxml xinclude