pythonxmlubl

How to access UBL 2.1 xml tag using python


I need to access the tags in UBL 2.1 and modify them depend on the on the user input on python.

So, I used the ElementTree library to access the tags and modify them.

Here is a sample of the xml code:

<ns0:Invoice xmlns:ns0="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
    <ns1:ProfileID>reporting:1.0</ns1:ProfileID>
    <ns1:ID>0</ns1:ID>
    <ns1:UUID>dbdf65eb-5d66-47e6-bb0c-a84bbf7baa30</ns1:UUID>
    <ns1:IssueDate>2022-11-05</ns1:IssueDate>

The issue :

I want to access the tags but it is doesn't modifed and enter the loop I tried both ways:

mytree = ET.parse('test.xml')
myroot = mytree.getroot()
for x in myroot.find({xmlns:ns1=urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2}IssueDate}"):
x.text = '1999'
mytree.write('test.xml')
mytree = ET.parse('test.xml')
myroot = mytree.getroot()
for x in myroot.iter('./Invoice/AllowanceCharge/ChargeIndicator'):
    x.text = str('true')
mytree.write('test.xml')
 

None of them worked and modify the tag.

So the questions is : How can I reach the specific tag and modify it?


Solution

  • If you correct the namespace and the brakets in your for loop it works for a valid XML like (root tag must be closed!):

    Input:

    <?xml version="1.0" encoding="utf-8"?>
    <ns0:Invoice xmlns:ns0="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
      <ns1:ProfileID>reporting:1.0</ns1:ProfileID>
      <ns1:ID>0</ns1:ID>
      <ns1:UUID>dbdf65eb-5d66-47e6-bb0c-a84bbf7baa30</ns1:UUID>
      <ns1:IssueDate>2022-11-05</ns1:IssueDate>
    </ns0:Invoice>
    

    Your repaired code:

    import xml.etree.ElementTree as ET
    
    tree = ET.parse('test.xml')
    root = tree.getroot()
    
    for elem in root.findall("{urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2}IssueDate"):
        elem.text = '1999'
    
    tree.write('test_changed.xml', encoding='utf-8', xml_declaration=True)    
    ET.dump(root)
    

    Output:

    <ns0:Invoice xmlns:ns0="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
        <ns1:ProfileID>reporting:1.0</ns1:ProfileID>
        <ns1:ID>0</ns1:ID>
        <ns1:UUID>dbdf65eb-5d66-47e6-bb0c-a84bbf7baa30</ns1:UUID>
        <ns1:IssueDate>1999</ns1:IssueDate>
    </ns0:Invoice>