I have a source XML data which contains the country name, I want to replace it with the country code. The country can be any country in the world. I have tried doing this using key function, but it keeps giving me null value in the final json output.
=== My Soap XML Data ===
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="urn:enter.soap.force.com"
xmlns:sf="urn:sobject.enter.soap.force.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<queryMoreResponse>
<result>
<records xsi:type="sf:CaseData">
<sf:Id>6896ADSD888</sf:Id>
<sf:Number>123456666</sf:Number>
<sf:ConsumerCountry>Australia</sf:ConsumerCountry>
</records>
<records xsi:type="sf:CaseData">
<sf:Id>6896SDRGGG666</sf:Id>
<sf:Number>123456666</sf:Number>
<sf:ConsumerCountry>United States</sf:ConsumerCountry>
</records>
</result>
</queryMoreResponse>
</soapenv:Body>
</soapenv:Envelope>
I have tried with 3 countries which can extend to more than 50 or 100
=== My XSLT 3.0 stylesheet ====
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sf="urn:sobject.enter.soap.force.com" xpath-default-namespace="urn:enter.soap.force.com"
xmlns:mf="http://example.com/mf" exclude-result-prefixes="#all">
<!-- starts: What I tried -->
<xsl:key name="countryCodes" match="sf:country" use="sf:name" />
<sf:country name="United States">US</sf:country>
<sf:country name="Australia">AU</sf:country>
<sf:country name="Germany">DE</sf:country>
<!-- ends: What I tried -->
<xsl:function name="mf:process-records" as="item()*">
<xsl:param name="elements" as="element()*"/>
<xsl:apply-templates select="$elements"/>
</xsl:function>
<xsl:output method="json" indent="yes"/>
<xsl:template match="/soapenv:Envelope">
<xsl:map>
<xsl:map-entry key="'caserecords'" select="array { mf:process-records(soapenv:Body/queryMoreResponse/result/records) }"/>
</xsl:map>
</xsl:template>
<xsl:template match="records">
<xsl:map-entry key="'case'">
<xsl:map>
<xsl:map-entry key="'report'">
<xsl:map>
<xsl:map-entry key="'reportid'" select="string(sf:Id)"/>
<xsl:map-entry key="'reportnumber'" select="string(sf:Number)"/>
<!-- starts: What I tried -->
<xsl:variable name="countryName" select="string(sf:ConsumerCountry)" />
<xsl:variable name="countryCode" select="key('countryCodes', $countryName)" />
<xsl:map-entry key="'sourcecountry'" select="$countryCode"/>
<!-- ends: What I tried -->
</xsl:map>
</xsl:map-entry>
</xsl:map>
</xsl:map-entry>
</xsl:template>
</xsl:stylesheet>
Expected Output
{
"records": [
{
"case": {
"safetyreport": {
"reportid": "6896ADSD888",
"reportnumber": "123456666",
"sourcecountry": "AU",
}
}
},
{
"case": {
"safetyreport": {
"reportid": "6896SDRGGG666",
"reportnumber": "123456666",
"sourcecountry": "US",
}
}
},
]
}
2
Answers
I would suggest (given that you use XSLT 3 and seem to want to define the mapping of country names to code inline) to declare a global map parameter e.g.
(in your XSLT sample you need to declare the prefix
xmlns:xs="http://www.w3.org/2001/XMLSchema"
) and then use e.g.Consider the following simplified example:
Applied to your input example, this will return: