[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSLT2 Change country name to alpha-2

Subject: Re: 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
Re:  XSLT2 Change country name to alpha-2
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.