Skip to content

Commit

Permalink
[#8] Add code to recreate Country codelist from ISO XML
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Feb 3, 2015
1 parent c7fc11a commit 6b1db9a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
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 Down Expand Up @@ -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)

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>

0 comments on commit 6b1db9a

Please sign in to comment.