【问题标题】:Stop ElementTree removing namespace from elements [duplicate]停止 ElementTree 从元素中删除命名空间 [重复]
【发布时间】:2018-03-31 18:50:24
【问题描述】:

我正在解析的 xml 文件中的一些元素有它们自己的 xmlns 属性,但是每当我解析并写回文件时,xmlns 都会被删除,而是得到一个 ns3: 前缀,并在顶。

我正在阅读的 XML 文件的头部:

<oval_definitions xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-common-5 http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#windows http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/windows-definitions-schema.xsd">

我得到的输出的头部:

<oval_definitions xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:ns3="http://oval.mitre.org/XMLSchema/oval-definitions-5#windows" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-common-5 http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#windows http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/windows-definitions-schema.xsd">

我的命名空间声明:

ET.register_namespace('', "http://oval.mitre.org/XMLSchema/oval-definitions-5")
ET.register_namespace('oval', "http://oval.mitre.org/XMLSchema/oval-common- 5")
ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
ET.register_namespace('xsi:schemaLocation', "http://oval.mitre.org/XMLSchema/oval-common-5 http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#windows http://oval.mitre.org/language/download/schema/version5.8/ovaldefinition/complete/windows-definitions-schema.xsd")

我想要什么:

<registry_state xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#windows" id="oval:mil.disa.fso.windows:ste:397100" version="2" comment="Reg_Dword type and value equals 0">

我现在得到的:

<ns3:registry_state comment="Reg_Dword type and value equals 0" id="oval:mil.disa.fso.windows:ste:397100" version="2">

如何将 xmlns= 属性重新添加到我的元素中并从文档头部取出?

【问题讨论】:

    标签: python xml elementtree


    【解决方案1】:

    oval:ns3: 是命名空间前缀,而不是命名空间。 命名空间前缀本身无关紧要;它只是通过 命名空间(例如http://oval.mitre.org/XMLSchema/oval-definitions-5#windows) 他们必须获得意义。没有兼容的 XML 处理器会关心特定的命名空间前缀(只关心它们绑定到的命名空间 URI),您或您编写的软件也不应该。

    同样,通过命名空间前缀控制默认命名空间与显式命名空间的使用也是一个没有区别的区别(假设在默认命名空间继承到后代元素时保留了等效性)。

    另请参阅:Why does xml package modify my xml file in Python3?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多