英文:
How can I set the namespace prefix when serializing with lxml.etree.xmlfile()?
问题
我尝试按顺序编写一个非常大的 XML 文件。以下是一个最小的代码示例:
import lxml.etree as ET
ET.register_namespace('xsl', 'http://www.w3.org/1999/XSL/Transform')
with ET.xmlfile('../output/test.xml', encoding='utf-8') as xf:
xf.write_declaration()
with xf.element(ET.QName('http://www.w3.org/1999/XSL/Transform', 'root')):
with xf.element('sub'):
pass
我得到了以下输出:
<?xml version='1.0' encoding='utf-8'?>
<ns0:root xmlns:ns0="http://www.w3.org/1999/XSL/Transform">
<sub>
</sub>
</ns0:root>
我希望前缀是 xsl:...,但实际上得到了通用的 ns0:...。是否有人知道我需要在这里进行哪些调整?
英文:
I try to write a very big xml file sequentially.
Here is a minimal code example:
import lxml.etree as ET
ET.register_namespace('xsl', 'http://www.w3.org/1999/XSL/Transform')
with ET.xmlfile('../output/test.xml', encoding='utf-8') as xf:
xf.write_declaration()
with xf.element(ET.QName('http://www.w3.org/1999/XSL/Transform', 'root')):
with xf.element('sub'):
pass
I get the follwing output.
<?xml version='1.0' encoding='utf-8'?>
<ns0:root xmlns:ns0="http://www.w3.org/1999/XSL/Transform">
<sub>
</sub>
</ns0:root>
I hoped the prefixes would be xsl:..., but instead got the generic ns0:... .
Does anbody know what I need to tweak here?
答案1
得分: 3
经过大量测试,我找到了正确的解决方案。需要在第一个元素上传递参数nsmap={tag: namespace}
,其中命名空间被使用。
import lxml.etree as ET
with ET.xmlfile('../output/test.xml', encoding='utf-8') as xf:
xf.write_declaration()
with xf.element(ET.QName('http://www.w3.org/1999/XSL/Transform', 'root'),
nsmap={'xsl': 'http://www.w3.org/1999/XSL/Transform'}):
with xf.element('sub'):
pass
具有相同命名空间的连续元素会自动从命名空间URI转换为标记表示法。然而,如果您始终传递nsmap参数,您将强制在每个元素上添加命名空间条目(xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
)。
英文:
After a lot of testing I found the right solution. One needs to pass the argument nsmap={tag : namespace}
on the first element, where the namespace is used.
import lxml.etree as ET
with ET.xmlfile('../output/test.xml', encoding='utf-8') as xf:
xf.write_declaration()
with xf.element(ET.QName('http://www.w3.org/1999/XSL/Transform', 'root'),
nsmap={'xsl': 'http://www.w3.org/1999/XSL/Transform'}):
with xf.element('sub'):
pass
Successive elements with the same namespace are automatically converted from namespace URI to tag notation. However, if you always pass the nsmap argument you would force a namespace entry on every element (xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论