我有一个包含国家/地区名称的源 XML 数据,我想将其替换为国家/地区代码。该国家可以是世界上的任何国家。我尝试使用 key 函数执行此操作,但它始终在最终 json 输出中给出 null 值。
=== 我的 Soap XML 数据 ===
<?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>
我尝试过 3 个国家,可以扩展到 50 或 100 多个
=== 我的 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>
预期输出
{
"records": [
{
"case": {
"safetyreport": {
"reportid": "6896ADSD888",
"reportnumber": "123456666",
"sourcecountry": "AU",
}
}
},
{
"case": {
"safetyreport": {
"reportid": "6896SDRGGG666",
"reportnumber": "123456666",
"sourcecountry": "US",
}
}
},
]
}