简体   繁体   English

多次定义的 lxml 命名空间

[英]lxml namespaces defined multiple times

I am switching from python xml to lxml.我正在从 python xml 切换到 lxml。 I faced that lxml has rather strict policy towards namespaces.我发现 lxml 对命名空间有相当严格的政策。

I want to produce a xml with multiple redundant xmlns:ns0 namespace declaration.我想生成一个带有多个冗余 xmlns:ns0 命名空间声明的 xml。 However, lxml strips this namespaces from child Elements.但是,lxml 从子元素中剥离了这个命名空间。

How can I define redundant namespaces via lxml?如何通过 lxml 定义冗余命名空间?

<?xml version="1.0" ?>
<env:Envelope xmlns:env="some URI" xmlns:ns0="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
        <ns0:getLabelResponseElement>
            <ns0:result>
                <ns1:statusMessage xmlns:ns0="http://www.w3.org/2001/XMLSchema"></ns1:statusMessage>
            </ns0:result>
        </ns0:getLabelResponseElement>
    </env:Body>
</env:Envelope>

Based on my research, lxml doesn't allow to add redundant namespace declaration in child Elements since this violates XML standards.根据我的研究,lxml 不允许在子元素中添加冗余命名空间声明,因为这违反了 XML 标准。 So, even if you explicitly redeclare namespace in child element lxml wouldn't add it in final XML.因此,即使您在子元素 lxml 中明确重新声明命名空间,也不会将其添加到最终的 XML 中。
Unlike lxml, python-xml library allows redundant xml namespace declaration.与 lxml 不同,python-xml 库允许冗余的 xml 命名空间声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM