'How to get ISO Country Code from Country Name in XSLT 2.0

Is there any built in function or way to get ISO Country code in XSLT 2.0?. I am getting a Country Name in the request and I have to pass country code in the outgoing request. One way is to map each country name to its respective Country Code, however this would take a lot of time so is there any shortcut to do that?



Solution 1:[1]

If you've got data like

<codes>
  <country code="gb" name="United Kingdom"/>
  <country code="de" name="Germany"/>
  ...
</codes>

then you typically define a key

<xsl:key name="ccode" match="country" use="@code"/>

and then you get the name of a country using

key('ccode', 'be', $country-codes)/@name

which returns "Belgium"

where $country-codes is the document containing this data.

Solution 2:[2]

In case other people might find this question while trying to solve the same issue as the original poster. I was able to solve this using all the details made available by @Michael-Kay

Source XML

<?xml version="1.0" encoding="utf-8"?>
<Document>
    <SalesOrder>        
        <Country>DK</Country>       
    </SalesOrder>
</Document>

XSLT / Stylesheet

<xsl:transform  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:variable name="countries">
        <countries>
            <country name="Cuba" alpha-2="CU" alpha-3="CUB" country-code="192" iso_3166-2="ISO 3166-2:CU" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Curaçao" alpha-2="CW" alpha-3="CUW" country-code="531" iso_3166-2="ISO 3166-2:CW" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Cyprus" alpha-2="CY" alpha-3="CYP" country-code="196" iso_3166-2="ISO 3166-2:CY" region="Asia" sub-region="Western Asia" intermediate-region="" region-code="142" sub-region-code="145" intermediate-region-code=""/>
            <country name="Czechia" alpha-2="CZ" alpha-3="CZE" country-code="203" iso_3166-2="ISO 3166-2:CZ" region="Europe" sub-region="Eastern Europe" intermediate-region="" region-code="150" sub-region-code="151" intermediate-region-code=""/>
            <country name="Denmark" alpha-2="DK" alpha-3="DNK" country-code="208" iso_3166-2="ISO 3166-2:DK" region="Europe" sub-region="Northern Europe" intermediate-region="" region-code="150" sub-region-code="154" intermediate-region-code=""/>
            <country name="Djibouti" alpha-2="DJ" alpha-3="DJI" country-code="262" iso_3166-2="ISO 3166-2:DJ" region="Africa" sub-region="Sub-Saharan Africa" intermediate-region="Eastern Africa" region-code="002" sub-region-code="202" intermediate-region-code="014"/>
            <country name="Dominica" alpha-2="DM" alpha-3="DMA" country-code="212" iso_3166-2="ISO 3166-2:DM" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Dominican Republic" alpha-2="DO" alpha-3="DOM" country-code="214" iso_3166-2="ISO 3166-2:DO" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Ecuador" alpha-2="EC" alpha-3="ECU" country-code="218" iso_3166-2="ISO 3166-2:EC" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="South America" region-code="019" sub-region-code="419" intermediate-region-code="005"/>
            <country name="Egypt" alpha-2="EG" alpha-3="EGY" country-code="818" iso_3166-2="ISO 3166-2:EG" region="Africa" sub-region="Northern Africa" intermediate-region="" region-code="002" sub-region-code="015" intermediate-region-code=""/>
            <country name="El Salvador" alpha-2="SV" alpha-3="SLV" country-code="222" iso_3166-2="ISO 3166-2:SV" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Central America" region-code="019" sub-region-code="419" intermediate-region-code="013"/>
        </countries>
    </xsl:variable>

    <xsl:key name="ccode" match="country" use="@alpha-2"/>

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Country has the value of DK -->
    <xsl:template match="Country">
        <xsl:copy>
            <xsl:value-of select="key('ccode', ., $countries)/@alpha-3" />
        </xsl:copy>     
    </xsl:template>
</xsl:transform>

Output XML

<?xml version="1.0" encoding="utf-8"?>
<Document>
    <SalesOrder>        
        <Country>DNK</Country>       
    </SalesOrder>
</Document>

Complete list of countries can be found here

Solution 3:[3]

Is there any built in function or way to get ISO Country code in XSLT 2.0?

No, of course not. Use a lookup table.

One way is to map each country name to its respective Country Code, however this would take a lot of time so is there any shortcut to do that?

It's been done already, for example: https://en.wikipedia.org/wiki/ISO_3166-1#Officially_assigned_code_elements

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Kay
Solution 2 Mötz
Solution 3 michael.hor257k