Tenho uma fonte de dados XML que contém o nome do país, desejo substituí-la pelo código do país. O país pode ser qualquer país do mundo. Eu tentei fazer isso usando a função de chave, mas continua me dando valor nulo na saída final do json.
=== Meus Dados XML do Soap ===
<?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>
Eu tentei com 3 países que podem se estender para mais de 50 ou 100
=== Minha folha de estilo XSLT 3.0 ====
<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>
Saída Esperada
{
"records": [
{
"case": {
"safetyreport": {
"reportid": "6896ADSD888",
"reportnumber": "123456666",
"sourcecountry": "AU",
}
}
},
{
"case": {
"safetyreport": {
"reportid": "6896SDRGGG666",
"reportnumber": "123456666",
"sourcecountry": "US",
}
}
},
]
}