I’m retrieving some XML-data containing countries among many other things and I would like to link the country name to its abbreviation in order to match them with famfamfam flags.
See the example below:
$('Result',xml).each(function(i) {
var $this = $(this),
user = $this.attr("Username"),
country = $this.attr("Country"),
So the variable country will fetch the country name, so how can I dynamically change the 247 potential countries to it’s abbreviation.
Below is a part of the list of the abbreviations and the full country names:
>FI FINLAND
>FJ FIJI
>FK FALKLAND ISLANDS (MALVINAS)
>FM MICRONESIA, FEDERATED STATES OF
>FO FAROE ISLANDS
>FR FRANCE
>FX FRANCE, METROPOLITAN
>GA GABON
>GB UNITED KINGDOM
>GE GEORGIA
You need to ask yourself the question whether or not the abbreviation is somehow stored in your xml-response. I assume it is not, in this case you would need to create an object in JavaScript like this:
Note: this in fact means that you will have a huge piece of code creating the countryCodes-array, which is more or less a database. Please consider putting in in a seperate js-file, called countries.js or whatever, and include if as (on of) the first javascript-file.
Now you can simply translate from a countryCode to the real name and the other way around, i.e.
will result in ‘Netherlands’ and
will result in ‘FR’.
A better option would be to determine what the most stable representation for a country is, I think that would be a country code, as names have different values in different languages. This way you could consider returning (server side) the countryCode as xml.
Anyroad, good luck!
NOTICE:
I just found this: http://www.geonames.org/export/ws-overview.html
It’s a nice list of webrequests you can do, to get geo info of all sorts. the countryInfo one seems to do exactly the opposite of what you want, but still it could help you. For example, this way you can get information on Germany. By letting the server respond with a code instead of the name, this might be just what you need!
http://api.geonames.org/countryInfoJSON?formatted=true&lang=it&country=DE&username=demo&style=full
If you will use this, don’t forget to make your own api.geoname.org account, instead of using the demo-username 😉