遍历XML文件以替换某些元素(Python)?

时间:2019-04-01 01:22:30

标签: python xml if-statement hl7-fhir

我试图遍历python中的某个XML文件以替换某些行。我对XML文件格式非常陌生,我不知道诸如elementTree或minidom之类的库如何工作。有问题的XML文件如下:

<?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&#xD;&#xA;YY" />
      <definition value="A coded type for the identifier that can be used to determine which identifier to use for a specific purpose.&#xD;&#xA;YY1" />
    </element>
    <element id="Patient.identifier.type.coding">
      <path value="Patient.identifier.type.coding" />
      <short value="Code defined by a terminology system&#xD;&#xA;XX" />
      <definition value="A reference to a code defined by a terminology system.&#xD;&#xA;XX2" />
    </element>
    <element id="Patient.maritalStatus.coding">
      <path value="Patient.maritalStatus.coding" />
      <short value="Code defined by a terminology system&#xD;&#xA;XX" />
      <definition value="A reference to a code defined by a terminology system.&#xD;&#xA;XX1" />
    </element>
    <element id="Patient.contact.relationship.coding">
      <path value="Patient.contact.relationship.coding" />
      <short value="Code defined by a terminology system&#xD;&#xA;XX" />
      <definition value="A reference to a code defined by a terminology system.&#xD;&#xA;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&#xD;&#xA;XX" />
      <definition value="A reference to a code defined by a terminology system.&#xD;&#xA;XX1" />
    </element>
  </differential>
</StructureDefinition>
如上所示,

有些地方我想用一些文本替换为XX(短值)或XX1(定义值)。此外,标有XX的位置均位于名称以“ .coding”结尾的某个元素的“短值”之下,而XX1均位于“定义值”(其为“差分”元素的所有子元素)下。我还不太清楚如何组织它,到目前为止,我的工作如下:

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

我无法获得循环以打印“ true”,因为“ res”是一个空列表。非常感谢您的帮助,在此先感谢您!

0 个答案:

没有答案