0

I am trying to modify XML file using xml.etree.ElementTree on Python 2.6.6 (due to restrictions) and facing ns0 issue. I looked at this issue and used ET._namespace_map[uri] = prefix as suggested which removed ns0 but the element tags still has the : value. How do we remove it or does it impact the validity of the XML file when we use if for further processing?

Example:

<?xml version="1.0" encoding="UTF-8" ?>
<Seed xmlns="http://www.example.com">
    <TagA>
        <TagB>B</TagB>
        <TagC>c</TagC>
    </TagA>
</Seed>

Script

import xml.etree.ElementTree as ET

tree = ET.parse('sample.xml')
root = tree.getroot()

try:
    ET.register_namespace("","http://example.com")
except AttributeError:
    def register_namespace(prefix, uri):
        ET._namespace_map[uri] = prefix
    register_namespace("","http://www.example.com")
tree.write('sample.xml')

Note: I could not use lxml or other xml.etree that is supported only from 2.7 version.

4
  • @mzjn The original file does not carry that notation. After we parse it with the xml.etree only, ":" is getting added. I am fine as long as, it does not invalidate the file. Commented Jul 10, 2020 at 3:58
  • What is "the original file"? Please provide a minimal reproducible example. Commented Jul 10, 2020 at 4:00
  • This might be a duplicate of stackoverflow.com/q/8113296/407651 Commented Jul 10, 2020 at 4:16
  • @mzjn Thanks for sharing the thread, looks like same issue. I will go through it. I have added sample XML data and script as requested. Commented Jul 10, 2020 at 5:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.