0

Bear with me here, I am trying to parse my "default_values.xml" with it's namespaces and root. And after that I want to extend root of "defaul_values.xml" with another xml file's root whose name is "test_values.xml", also writing that combinated xml to "final_values.xml" file.

So far I did extending (or merging) process but I'm having problem on namespaces in "final.xml" file. My Python code doesn't write namespaces as I want. Here is xml files and code, also xml file which I want to create in the final step:

"default_values.xml"

<?xml version="1.0" encoding="UTF-8"?>
<myMainRoot xmlns:ns1="http://www.examplenamespace1.com/v123#"
    xmlns:ns2="http://www.examplenamespace2.com/v123#"
    xmlns:ns3="http://www.examplenamespace3.com/v123#"
    xmlns="http://www.examplenamespace4.com"
    xmlns:xsi="http://www.examplenamespace4.com/v123">
    <title>
        <version>1.0</version>
        <realMadrid>
            <Ronaldo>Portugal</Ronaldo>
        </realMadrid>
        <barcelona>
            <Messi>Argentina</Messi>
        </barcelona>
        <teamNo>12-13-14</teamNo>
        <gameDate>2017-04-23</gameDate>

        <ns2:abc>
            <ns2:abc1>
                <ns2:abc2 Algorithm="http://www.examplenamespace5.com/">
                </ns2:abc2>
                <ns2:abc3 Algorithm="http://www.examplenamespace5.com/">
                </ns2:abc3>
                <ns2:abc4>
                    <ns2:abc5 Algorithm="http://www.examplenamespace5.com/">
                    </ns2:abc5>
                    <ns2:abc6>ZGVmYXVsdA==</ns2:abc6>
                </ns2:abc4>
            </ns2:abc1>
        </ns2:abc>
    </title>
</myMainRoot>

"test_values.xml"

<myChildRoot>
    <jerseyNo>7</jerseyNo>
    <transferTime>11.09.2009</transferTime>
    <scoreNo>
        <year1>23</year1>
        <year2>22</year2>
        <year3>21.0</year3>
    </scoreNo>
    <scoreNo>
        <year1>43</year1>
        <year2>42</year2>
        <year3>41.0</year3>
    </scoreNo>
    <finalNo>2</finalNo>
</myChildRoot>

"final_values.xml"

<?xml version='1.0' encoding='UTF-8'?>
<ns0:myMainRoot xmlns:ns0="http://www.examplenamespace4.com" xmlns:ns1="http://www.examplenamespace2.com/v123#">
    <ns0:title>
        <ns0:version>1.0</ns0:version>
        <ns0:realMadrid>
            <ns0:Ronaldo>Portugal</ns0:Ronaldo>
        </ns0:realMadrid>
        <ns0:barcelona>
            <ns0:Messi>Argentina</ns0:Messi>
        </ns0:barcelona>
        <ns0:teamNo>12-13-14</ns0:teamNo>
        <ns0:gameDate>2017-04-23</ns0:gameDate>

        <ns1:abc>
            <ns1:abc1>
                <ns1:abc2 Algorithm="http://www.examplenamespace5.com/">
                </ns1:abc2>
                <ns1:abc3 Algorithm="http://www.examplenamespace5.com/">
                </ns1:abc3>
                <ns1:abc4>
                    <ns1:abc5 Algorithm="http://www.examplenamespace5.com/">
                    </ns1:abc5>
                    <ns1:abc6>ZGVmYXVsdA==</ns1:abc6>
                </ns1:abc4>
            </ns1:abc1>
        </ns1:abc>
    </ns0:title>
    <extended>
        <jerseyNo>7</jerseyNo>
        <transferTime>11.09.2009</transferTime>
        <scoreNo>
            <year1>23</year1>
            <year2>22</year2>
            <year3>21.0</year3>
        </scoreNo>
        <scoreNo>
            <year1>43</year1>
            <year2>42</year2>
            <year3>41.0</year3>
        </scoreNo>
        <finalNo>2</finalNo>
    </extended>
</ns0:myMainRoot>

Also my Python code:

import xml.etree.ElementTree as ET

tree_of_default_values_xml = ET.parse('1.xml')
root_of_default_values_xml = tree_of_default_values_xml.getroot()

tree_of_data_block_xml = ET.parse("2.xml")
root_of_data_block_xml = tree_of_data_block_xml.getroot()
subelement_to_extend_for_parent_xml_root = ET.SubElement(root_of_default_values_xml, "extended")
subelement_to_extend_for_parent_xml_root.extend(root_of_data_block_xml)
tree_of_default_values_xml.write("3.xml", encoding="UTF-8", xml_declaration=True)

Lastly, that is the result which I want to do for "final.xml":

<?xml version="1.0" encoding="UTF-8"?>
<myMainRoot xmlns:ns1="http://www.examplenamespace1.com/v123#"
    xmlns:ns2="http://www.examplenamespace2.com/v123#"
    xmlns:ns3="http://www.examplenamespace3.com/v123#"
    xmlns="http://www.examplenamespace4.com"
    xmlns:xsi="http://www.examplenamespace4.com/v123">
    <title>
        <version>1.0</version>
        <realMadrid>
            <Ronaldo>Portugal</Ronaldo>
        </realMadrid>
        <barcelona>
            <Messi>Argentina</Messi>
        </barcelona>
        <teamNo>12-13-14</teamNo>
        <gameDate>2017-04-23</gameDate>
        <ns2:abc>
            <ns2:abc1>
                <ns2:abc2 Algorithm="http://www.examplenamespace5.com/">
                </ns2:abc2>
                <ns2:abc3 Algorithm="http://www.examplenamespace5.com/">
                </ns2:abc3>
                <ns2:abc4>
                    <ns2:abc5 Algorithm="http://www.examplenamespace5.com/">
                    </ns2:abc5>
                    <ns2:abc6>ZGVmYXVsdA==</ns2:abc6>
                </ns2:abc4>
            </ns2:abc1>
        </ns2:abc>
    </title>
    <extended>
        <jerseyNo>7</jerseyNo>
        <transferTime>11.09.2009</transferTime>
        <scoreNo>
            <year1>23</year1>
            <year2>22</year2>
            <year3>21.0</year3>
        </scoreNo>
        <scoreNo>
            <year1>43</year1>
            <year2>42</year2>
            <year3>41.0</year3>
        </scoreNo>
        <finalNo>2</finalNo>
    </extended>
</myMainRoot>
fenderogi
  • 118
  • 7
  • "but I'm having problem on namespaces" - What problems? – mzjn Jul 29 '21 at 08:03
  • My extended "final.xml" file doesn't look like what I want as a result. Sample files are above. I edited the post, it might help people to understand easily. – fenderogi Jul 29 '21 at 08:07
  • The `ns1` and `ns3` namespaces are not used in default_values.xml. ElementTree removes declarations for unused namespaces. See https://stackoverflow.com/q/45990761/407651. And in order to set a specific prefix (or no prefix at all), use `register_namespace`. See for example https://stackoverflow.com/a/68470618/407651) – mzjn Jul 29 '21 at 08:11
  • I've been trying to come through but I am making no headway. – fenderogi Jul 29 '21 at 08:46
  • If you can, use lxml instead of ElementTree. Working with namespaces is easier with lxml. – mzjn Jul 29 '21 at 08:52

0 Answers0