1

I am trying to modify two parameters in the file below (this is just an example, I have a much larger file to modify).

<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="urn:cmp:ran:cmp_data_container:5_0_326_21">
  <id>1</id>
  <ManagedElement xmlns="urn:std:sa5:managed-element:5_0_326_21">
    <id>1</id>
    <attributes>
      <priorityLabel>1</priorityLabel>
    </attributes>
    <GNBDUFunction xmlns="urn:std:sa5:gnbdufunction:5_0_326_21">
      <id>1</id>
      <attributes>
        <userLabel/>
        <resourceType/>
        <rRMPolicyMemberList>
          <idx>1</idx>
          <mcc>262</mcc>
          <mnc>01</mnc>
          <sNSSAI>50519801</sNSSAI>
        </rRMPolicyMemberList>
        <gNBId>83030094</gNBId>
        <gNBIdLength>28</gNBIdLength>
        <gNBDUId>1</gNBDUId>
        <gNBDUName>mBTS_121</gNBDUName>
      </attributes>
    </GNBDUFunction>
  </ManagedElement>
</data>

But the output .xml file has the namespaces in the top of the file only and from what I see, those are mapped in the file with ns0, ns1 etc.

The output file:


<ns0:data xmlns="urn:std:sa5:gnbdufunction:5_0_326_21" xmlns:ns0="urn:cmp:ran:cmp_data_container:5_0_326_21" xmlns:ns1="urn:std:sa5:managed-element:5_0_326_21">
  <ns0:id>1</ns0:id>
  <ns1:ManagedElement>
    <ns1:id>1</ns1:id>
    <ns1:attributes>
      <ns1:priorityLabel>1</ns1:priorityLabel>
    </ns1:attributes>
    <GNBDUFunction>
      <id>1</id>
      <attributes>
        <userLabel />
        <resourceType />
        <rRMPolicyMemberList>
          <idx>1</idx>
          <mcc>262</mcc>
          <mnc>01</mnc>
          <sNSSAI>50519801</sNSSAI>
        </rRMPolicyMemberList>
        <gNBId>9999999</gNBId>
        <gNBIdLength>9</gNBIdLength>
        <gNBDUId>1</gNBDUId>
        <gNBDUName>mBTS_121</gNBDUName>
      </attributes>
    </GNBDUFunction>
  </ns1:ManagedElement>
</ns0:data>


The code:

import xml.etree.ElementTree as ET

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

for lvl1 in root:
for lvl2 in lvl1:
extracted = lvl2.tag.split('}')\[0\].strip('{')
ET.register_namespace('', extracted)
aux = '{'+extracted+'}'+'GNBDUFunction'

        if lvl2.tag == aux:
            for lvl3 in lvl2:
                extracted = lvl3.tag.split('}')[0].strip('{')
                ET.register_namespace('', extracted)
                aux = '{'+extracted+'}'+'attributes'
    
                if lvl3.tag == aux:
                    for lvl4 in lvl3:
                        extracted = lvl4.tag.split('}')[0].strip('{')
                        ET.register_namespace('', extracted)
                        gnbid = '{'+extracted+'}'+'gNBId'
                        gnbidlength = '{'+extracted+'}'+'gNBIdLength'
    
                        if lvl4.tag == gnbid:
                            lvl4.text = str(9999999)
                        if lvl4.tag == gnbidlength:
                            lvl4.text = str(9)

tree.write('example_output.xml')

The thing is, I need the output .xml file to be the same format as the initial one.

I tried to register all namespaces manually just to see if I am on the right track with registering namespaces, but is the same output.

ET.register_namespace('', "urn:3gpp:sa5:3gpp-nr-nrm-nrnetwork-rrmpolicy:5_0_326_27")
ET.register_namespace('ns0', "urn:mavenir:ran:mavenir_data_container:5_0_326_27")
ET.register_namespace('ns1', "urn:3gpp:sa5:_3gpp-common-managed-element:5_0_326_27")
ET.register_namespace('ns2', "urn:3gpp:sa5:_3gpp-nr-nrm-gnbdufunction:5_0_326_27")
ET.register_namespace('ns3', "urn:3gpp:sa5:_3gpp-nr-nrm-nrcelldu:5_0_326_27")
ET.register_namespace('ns4', "urn:3gpp:sa5:_3gpp-nr-nrm-bwp:5_0_326_27")
ET.register_namespace('ns5', "urn:3gpp:sa5:_3gpp-nr-nrm-ep:5_0_326_27")

The output I want to obtain is like this:

<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="urn:cmp:ran:cmp_data_container:5_0_326_21">
  <id>1</id>
  <ManagedElement xmlns="urn:std:sa5:managed-element:5_0_326_21">
    <id>1</id>
    <attributes>
      <priorityLabel>1</priorityLabel>
    </attributes>
    <GNBDUFunction xmlns="urn:std:sa5:gnbdufunction:5_0_326_21">
      <id>1</id>
      <attributes>
        <userLabel/>
        <resourceType/>
        <rRMPolicyMemberList>
          <idx>1</idx>
          <mcc>262</mcc>
          <mnc>01</mnc>
          <sNSSAI>50519801</sNSSAI>
        </rRMPolicyMemberList>
        <gNBId>9999999</gNBId>
        <gNBIdLength>9</gNBIdLength>
        <gNBDUId>1</gNBDUId>
        <gNBDUName>mBTS_121</gNBDUName>
      </attributes>
    </GNBDUFunction>
  </ManagedElement>
</data>

Please let me know if there is a way to obtain this.

P.S. I understand that this maybe is not the best approach to the problem. This is the first time doing this type of project.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Dani
  • 11
  • 2

0 Answers0