Reputation: 17
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",
}
}
},
]
}
Upvotes: 0
Views: 301
Reputation: 117140
Consider the following simplified example:
<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:my="http://example.cpm/my"
exclude-result-prefixes="#all">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="countryCode" match="my:country" use="@name" />
<xsl:variable name="countryCodes">
<my:country name="United States">US</my:country>
<my:country name="Australia">AU</my:country>
<my:country name="Germany">DE</my:country>
</xsl:variable>
<xsl:template match="/soapenv:Envelope">
<root>
<xsl:apply-templates select="soapenv:Body/queryMoreResponse/result/records"/>
</root>
</xsl:template>
<xsl:template match="records">
<record>
<country>
<xsl:value-of select="sf:ConsumerCountry"/>
</country>
<countryCode>
<xsl:value-of select="key('countryCode', sf:ConsumerCountry, $countryCodes)"/>
</countryCode>
</record>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, this will return:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<record>
<country>Australia</country>
<countryCode>AU</countryCode>
</record>
<record>
<country>United States</country>
<countryCode>US</countryCode>
</record>
</root>
Upvotes: 1
Reputation: 167716
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.
<xsl:param name="countryCodes" as="map(xs:string,xs:string)"
select="map {
'United States' : 'US',
'Australia' : 'AU',
'Germany' : 'DE'
}"/>
(in your XSLT sample you need to declare the prefix xmlns:xs="http://www.w3.org/2001/XMLSchema"
) and then use e.g.
<xsl:map-entry key="'sourcecountry'" select="$countryCodes(sf:ConsumerCountry)"/>
Upvotes: 0