Skip to content

Commit

Permalink
[#8] Recreate Country codelist from ISO XML
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Jun 9, 2014
1 parent 6923d2b commit 23f3fff
Show file tree
Hide file tree
Showing 3 changed files with 1,852 additions and 1,008 deletions.
48 changes: 47 additions & 1 deletion convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -29,3 +35,43 @@

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)

7 changes: 7 additions & 0 deletions templates/Country.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<codelist name="Country" xml:lang="en" complete="0">
<metadata>
<name>Country</name>
<url>http://www.iso.org/iso/home/standards/country_codes.htm</url>
</metadata>
<codelist-items/>
</codelist>
Loading

0 comments on commit 23f3fff

Please sign in to comment.