Re: [xsl] XSLT2 Change country name to alpha-2

Subject: Re: [xsl] XSLT2 Change country name to alpha-2
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 2 Jul 2023 11:03:07 -0000
On 02.07.2023 12:51, LEGAULT, PHILLIP plegault@xxxxxxxxxx wrote:
>
> Is there an way to convert country name to alpha-2?
>

You could consider a REST API that gives you the information; most REST
API these days are primarily returning JSON as the response so
processing with XSLT 3 instead of XSLT 2 is easier e.g.


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 B  version="3.0"
 B  xmlns:xs="http://www.w3.org/2001/XMLSchema";
 B  exclude-result-prefixes="#all"
 B  expand-text="yes">

 B  <xsl:template match="country">
 B B B  <xsl:copy>
 B B B B B  <name>{.}</name>
 B B B B B  <cca2>{json-doc('https://restcountries.com/v3.1/name/' || . ||
'?fields=cca2')?*?cca2}</cca2>
 B B B  </xsl:copy>
 B  </xsl:template>

 B  <xsl:mode on-no-match="shallow-copy"/>

</xsl:stylesheet>


for the input


<countries>
 B  <country>USA</country>
 B  <country>Deutschland</country>
</countries>


gives me

<countries>
<country><name>USA</name><cca2>US</cca2></country>
<country><name>Deutschland</name><cca2>DE</cca2></country>
</countries>


Of course if a REST API has an option to return XML instead of JSON you
can easily `doc` to "call it" and then use XPath 2 expressions on the
returned document to extract the code.

Current Thread