我想修改XML文件的zip存档中的所有文件。当我尝试这样做时,它告诉我该文件不存在。如果我注释掉替换呼叫,虽然这有效。我做错了什么?
Zip::ZipFile.open(temp_file.path) do |zipfile|
for i in 0..(zipfile.entries.count - 1)
entry = zipfile.entries[i]
if entry.name
if entry.name.split("/").last == "patient_manifest.txt"
next
end
end
next if entry.directory?
data = zipfile.read(entry.name)
doc = Nokogiri::XML(data)
doc.root.add_namespace_definition('cda', 'urn:hl7-org:v3')
doc.root.add_namespace_definition('sdtc', 'urn:hl7-org:sdtc')
patient_role_element = doc.at_xpath('/cda:ClinicalDocument/cda:recordTarget/cda:patientRole')
patient_element = patient_role_element.at_xpath('./cda:patient')
first = patient_element.at_xpath('cda:name/cda:given').text
last = patient_element.at_xpath('cda:name/cda:family').text
id_node = patient_role_element.at_xpath('./cda:id')
id_node['extension'] = id_node['extension'] + first + last
xml_file = Tempfile.new('foo')
begin
xml_file.write(doc.to_xml)
zipfile.replace(entry, xml_file.path)
ensure
xml_file.close
xml_file.unlink # deletes the temp file
end
end
end