From 6b1db9a285526b692ac481eb06b785e5880e4a55 Mon Sep 17 00:00:00 2001 From: Ben Webb Date: Mon, 9 Jun 2014 12:06:49 +0100 Subject: [PATCH] [#8] Add code to recreate Country codelist from ISO XML --- convert.py | 48 ++++++++++++++++++++++++++++++++++++++++++- templates/Country.xml | 7 +++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 templates/Country.xml diff --git a/convert.py b/convert.py index cfdbb9f3..a75f6498 100644 --- a/convert.py +++ b/convert.py @@ -2,12 +2,18 @@ Converts codelist files from external sources into the format used by IATI. -Currently only supports the IANA Media Types code list (FileFormat). +Note not all external codelists are converted automatically yet. """ from lxml import etree as ET +""" + +IANA Media Types (FileFormat) + +""" + template = ET.parse('templates/FileFormat.xml', ET.XMLParser(remove_blank_text=True)) codelist_items = template.find('codelist-items') @@ -47,3 +53,43 @@ def indent(elem, level=0, shift=2): template.write('xml/FileFormat.xml', pretty_print=True) + + +""" + +ISO Country Alpha 2 + +""" + +XML_LANG = '{http://www.w3.org/XML/1998/namespace}lang' + +template = ET.parse('templates/Country.xml', ET.XMLParser(remove_blank_text=True)) +codelist_items = template.find('codelist-items') + +countries = ET.parse('source/iso_country_codes.xml') +for country in countries.findall('country'): + if country.find('status').text == 'officially-assigned': + codelist_item = ET.Element('codelist-item') + + code = ET.Element('code') + code.text = country.find('alpha-2-code').text + codelist_item.append(code) + + for short_name in country.findall('short-name'): + if XML_LANG in short_name.attrib: + name = ET.Element('name') + name.attrib[XML_LANG] = short_name.attrib[XML_LANG] + name.text = short_name.text + codelist_item.append(name) + + for full_name in country.findall('full-name'): + if XML_LANG in full_name.attrib: + description = ET.Element('description') + description.attrib[XML_LANG] = full_name.attrib[XML_LANG] + description.text = full_name.text + codelist_item.append(description) + + codelist_items.append(codelist_item) + +template.write('xml/Country.xml', pretty_print=True) + diff --git a/templates/Country.xml b/templates/Country.xml new file mode 100644 index 00000000..5e6efe58 --- /dev/null +++ b/templates/Country.xml @@ -0,0 +1,7 @@ + + + Country + http://www.iso.org/iso/home/standards/country_codes.htm + + +