I am trying to loop through a certain XML file in python in order to replace certain lines. I am very new to XML file formats and I dont know how libraries such as elementTree or minidom works. The XML file in question is as followed:
<?xml version="1.0" encoding="utf-8"?>
<StructureDefinition xmlns="http://hl7.org/fhir">
<url value="http://example.org/fhir/StructureDefinition/MyPatient" />
<name value="MyPatient" />
<status value="draft" />
<fhirVersion value="3.0.1" />
<kind value="resource" />
<abstract value="false" />
<type value="Patient" />
<baseDefinition value="http://hl7.org/fhir/StructureDefinition/Patient" />
<derivation value="constraint" />
<differential>
<element id="Patient.identifier.type">
<path value="Patient.identifier.type" />
<short value="Description of identifier
YY" />
<definition value="A coded type for the identifier that can be used to determine which identifier to use for a specific purpose.
YY1" />
</element>
<element id="Patient.identifier.type.coding">
<path value="Patient.identifier.type.coding" />
<short value="Code defined by a terminology system
XX" />
<definition value="A reference to a code defined by a terminology system.
XX2" />
</element>
<element id="Patient.maritalStatus.coding">
<path value="Patient.maritalStatus.coding" />
<short value="Code defined by a terminology system
XX" />
<definition value="A reference to a code defined by a terminology system.
XX1" />
</element>
<element id="Patient.contact.relationship.coding">
<path value="Patient.contact.relationship.coding" />
<short value="Code defined by a terminology system
XX" />
<definition value="A reference to a code defined by a terminology system.
XX1" />
</element>
<element id="Patient.contact.organization.identifier.type.coding">
<path value="Patient.contact.organization.identifier.type.coding" />
<short value="Code defined by a terminology system
XX" />
<definition value="A reference to a code defined by a terminology system.
XX1" />
</element>
</differential>
</StructureDefinition>
as you can see above, there are places marked with XX (short value), or XX1 (definition value) that I want to replace with some text. Furthermore, the places marked with XX are all under 'short value' of some element with name ending in '.coding', and XX1 are all under 'definition value' (which is all sub-elements of the 'differential' element). I can't quite figure out how to organize this, what I have so far is as below:
from xml.dom.minidom import parse # use minidom for this task
dom = parse('C:/Users/Joon/Documents/FHIR/MyPatient.StructureDefinition.xml') #read in your file
replace1 = "replacement text" #set replace value
replace2 = "replacement text2" #set replace value2
res = dom.getElementsByTagName('*.coding') #iterate over tags
for element in res:
print('true')
element.setAttribute('short value', replace1)
element.setAttribute('definition value', replace2)
with open('MyPatientUpdated.StructureDefinition.xml', 'w', encoding='UTF8') as f:
f.write(dom.toxml()) #update the dom, save as new xml file
I can't get the loop to print 'true' because 'res' is an empty list. Any help is greatly appreciated, thank you in advance!
Aucun commentaire:
Enregistrer un commentaire