text: Q:
  
  How to replace xml lines using 'if statements' in python?
  
  Hi I'm new to xml files in general, but I am trying to replace specific lines in a xml file using 'if statements' in python 3.6. I've been looking at suggestions to use ElementTree, but none of the posts online quite fit the problem I have, so here I am.
  My file is as followed:
  <?xml version="1.0" encoding="UTF-8"?>
  
  -<StructureDefinition xmlns="http://hl7.org/fhir">
  
      <url value="http://example.org/fhir/StructureDefinition/MyObservation"/>
  
      <name value="MyObservation"/>
  
      <status value="draft"/>
  
      <fhirVersion value="3.0.1"/>
  
      <kind value="resource"/>
  
      <abstract value="false"/>
  
      <type value="Observation"/>
  
      <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Observation"/>
  
      <derivation value="constraint"/>
  
  </StructureDefinition>
  
  I want to replace 
  url value="http://example.org/fhir/StructureDefinition/MyObservation"/
  
  to something like
  url value="http://example.org/fhir/StructureDefinition/NewObservation"/
  
  by using conditional statements - because these are repeated multiple times in other files.
  I have tried for-looping through the xml find to find the exact string match (which I've succeeded), but I wasn't able to delete, or replace the line (probably having to do with the fact that this isn't a .txt file).
  Any help is greatly appreciated!
  
  A:
  
  Your sample file contains a "-"-token in ln 3 that may be overlooked when copy/pasting in order to find a solution.
  Input File
  <?xml version="1.0" encoding="UTF-8"?>
  
  <StructureDefinition xmlns="http://hl7.org/fhir">
  
      <url value="http://example.org/fhir/StructureDefinition/MyObservation"/>
  
      <name value="MyObservation"/>
  
      <status value="draft"/>
  
      <fhirVersion value="3.0.1"/>
  
      <kind value="resource"/>
  
      <abstract value="false"/>
  
      <type value="Observation"/>
  
      <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Observation"/>
  
      <derivation value="constraint"/>
  
  </StructureDefinition>
  
  Script
  from xml.dom.minidom import parse # use minidom for this task
  dom = parse('june.xml') #read in your file
  search = "http://example.org/fhir/StructureDefinition/MyObservation" #set search value
  replace = "http://example.org/fhir/StructureDefinition/NewObservation" #set replace value
  res = dom.getElementsByTagName('url') #iterate over url tags
  for element in res:
      if element.getAttribute('value') == search: #in case of match
          element.setAttribute('value', replace) #replace
  with open('june_updated.xml', 'w') as f:
      f.write(dom.toxml()) #update the dom, save as new xml file
  
  Output file
  <?xml version="1.0" ?><StructureDefinition xmlns="http://hl7.org/fhir">
  
      <url value="http://example.org/fhir/StructureDefinition/NewObservation"/>
  
      <name value="MyObservation"/>
  
      <status value="draft"/>
  
      <fhirVersion value="3.0.1"/>
  
      <kind value="resource"/>
  
      <abstract value="false"/>
  
      <type value="Observation"/>
  
      <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Observation"/>
  
      <derivation value="constraint"/>
  
  </StructureDefinition>
  
  