title: Problem removing parent element in a XML file with Python
body: <p>I'm writing a little python script that parses XML file and outputs modified file that I need. Everything works, but I have a problem with one part removing parent. It removes it, but leaves closing element.</p>
  <p>This is part of the file:</p>
  <pre><code>&lt;cbc:DocumentCurrencyCode&gt;RSD&lt;/cbc:DocumentCurrencyCode&gt;
    &lt;cac:InvoicePeriod&gt;
      &lt;cbc:DescriptionCode&gt;35&lt;/cbc:DescriptionCode&gt;
    &lt;/cac:InvoicePeriod&gt;
    &lt;cac:ContractDocumentReference&gt;
      &lt;cbc:ID&gt;40&lt;/cbc:ID&gt;
    &lt;/cac:ContractDocumentReference&gt;
    &lt;cac:AccountingSupplierParty&gt;
      &lt;cac:Party&gt;
        &lt;cbc:EndpointID schemeID=&quot;9948&quot;&gt;109657296&lt;/cbc:EndpointID&gt;
      &lt;/cac:Party&gt;
    &lt;/cac:AccountingSupplierParty&gt;
    &lt;cac:AccountingCustomerParty&gt;
  </code></pre>
  <p>This is the part of the file that I want to modify. I have a code that should remove this part</p>
  <pre><code>  &lt;cac:ContractDocumentReference&gt;
      &lt;cbc:ID&gt;40&lt;/cbc:ID&gt;
    &lt;/cac:ContractDocumentReference&gt;
  </code></pre>
  <p>I use this code for finding element, modifying it with if statement, and removing it if line starts with JBKJS</p>
  <pre><code>for EndpointID in root2.findall('.//cac:ContractDocumentReference/cbc:ID', ns):
              if str(eFaktura.find('BPG').text).startswith('JBKJS'):
                  EndpointID.text = str(eFaktura.find('BrRac').text)
              else:
                  EndpointID.getparent().remove(EndpointID)
  </code></pre>
  <p>The problem is, if the field is not populated, it does remove element, but not fully, so I'm left with the file like this:</p>
  <pre><code>&lt;cbc:DocumentCurrencyCode&gt;RSD&lt;/cbc:DocumentCurrencyCode&gt;
    &lt;cac:InvoicePeriod&gt;
      &lt;cbc:DescriptionCode&gt;35&lt;/cbc:DescriptionCode&gt;
    &lt;/cac:InvoicePeriod&gt;
    &lt;/cac:ContractDocumentReference&gt;
    &lt;cac:AccountingSupplierParty&gt;
      &lt;cac:Party&gt;
        &lt;cbc:EndpointID schemeID=&quot;9948&quot;&gt;109657296&lt;/cbc:EndpointID&gt;
      &lt;/cac:Party&gt;
    &lt;/cac:AccountingSupplierParty&gt;
    &lt;cac:AccountingCustomerParty&gt;
  </code></pre>
  <p>It doesn't remove <code>&lt;/cac:ContractDocumentReference&gt;</code></p>
  <p>I googled for days now, and cannot find the answer why is this happening</p>