私が解析しているxmlファイルの要素の中には、独自のxmlns属性を持っていますが、ファイルを解析して書き込むたびにxmlnsが削除され、代わりにns3:新しい名前空間が先頭に追加されます。Stop ElementTree要素から名前空間を削除する
私が読んでいる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")
を何I欲しいもの:
<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属性を得ることができますか?
上記は正しいですが、私はOPと思います。■主な問題は、ElementTreeのは迷惑な方法で 名前空間宣言とフィドルということです。 https://stackoverflow.com/q/45990761/407651、https://stackoverflow.com/q/38438921/407651,https://stackoverflow.com/q/41395332/407651を参照してください。 – mzjn
@mzjn:ElementTreeはそのような変更を行うために完全にその範囲内にあります。私はいくつかのレベルで、それは少し非理想的な驚くべきことであることに同意する。私はあなたの答えへのリンクを参考にして追加しました。ありがとうございました。 – kjhughes