0

Some of my elements in the xml file I am parsing have their own xmlns attribute, but whenever I parse and write the file back, the xmlns are removed and instead I get a ns3: prefix and a new namespace is added at the top.

The head of the XML file I'm reading:

<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">

The head of the output I get:

<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">

My namespace declarations:

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")

What I want:

<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">

What I'm getting now:

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

How can I get the xmlns= attribute back into my elements and out of the head of the document?

1 Answers1

0

oval: and ns3: are namespace prefixes, not namespaces. Namespace prefixes themselves are insignificant; it is only through the namespace (eg http://oval.mitre.org/XMLSchema/oval-definitions-5#windows) to which they're bound that they derive meaning. No compliant XML processors will care about the specific namespace prefix (only the namespace URIs to which they're bound), and neither should you or the software you write.

Similarly, control over the use of a default namespace vs an explicit namespace via an namespace prefix is also a difference that makes no difference (assuming equivalence is preserved wrt the inheritance of the default namespaces to descendant elements).

See also: Why does xml package modify my xml file in Python3?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • The above is correct, but I think the OP:s main problem is that ElementTree fiddles with namespace declarations in an annoying way. See https://stackoverflow.com/q/45990761/407651, https://stackoverflow.com/q/38438921/407651, https://stackoverflow.com/q/41395332/407651. – mzjn Oct 19 '17 at 17:08
  • 1
    @mzjn: ElementTree is perfectly within its right to make such changes. I do agree, however, that at some level, it's surprising a a bit non-ideal. I've added a link to your answer for reference. Thank you. – kjhughes Oct 19 '17 at 17:25