如何使用XSLT 3.0 key函数实现国家名转国家码?
解决XSLT3.0中使用key函数替换国家名为国家码时字段为空的问题
问题背景
有一份包含国家名称的SOAP XML数据源,需将其中的sf:ConsumerCountry字段值替换为对应国家码,尝试使用XSLT3.0的key函数实现,但最终JSON输出中sourcecountry字段始终为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>
原XSLT3.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" } } } ] }
错误原因分析
- key函数上下文不匹配:原代码直接将
sf:country元素放在样式表顶层,这些元素不属于主输入文档的节点集,而key函数默认仅针对主输入文档节点匹配,因此无法找到映射关系。 - 取值逻辑错误:即使匹配到
sf:country元素,原代码直接将元素本身赋值给字段,而实际需要的是元素的文本内容(国家码)。 - 结构键名不符:原代码根键为
caserecords、内层键为report,与预期输出的records、safetyreport不匹配,导致输出结构不符合要求。
修正后的XSLT3.0样式表
方案一:使用XSLT3.0 xsl:map存储映射(推荐,易扩展)
<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" exclude-result-prefixes="#all"> <!-- 定义国家名称到国家码的映射 --> <xsl:variable name="countryMap" as="map(xs:string, xs:string)"> <xsl:map> <xsl:map-entry key="'United States'" select="'US'"/> <xsl:map-entry key="'Australia'" select="'AU'"/> <xsl:map-entry key="'Germany'" select="'DE'"/> <!-- 后续可直接添加更多国家映射 --> </xsl:map> </xsl:variable> <xsl:output method="json" indent="yes"/> <xsl:template match="/soapenv:Envelope"> <xsl:map> <xsl:map-entry key="'records'" select="array { soapenv:Body/queryMoreResponse/result/records ! mf:process-record(.) }"/> </xsl:map> </xsl:template> <xsl:function name="mf:process-record" as="map(*)"> <xsl:param name="record" as="element()"/> <xsl:map> <xsl:map-entry key="'case'"> <xsl:map> <xsl:map-entry key="'safetyreport'"> <xsl:map> <xsl:map-entry key="'reportid'" select="string($record/sf:Id)"/> <xsl:map-entry key="'reportnumber'" select="string($record/sf:Number)"/> <!-- 从映射中获取国家码,无匹配时可返回原名称或空 --> <xsl:map-entry key="'sourcecountry'" select="$countryMap(string($record/sf:ConsumerCountry))"/> </xsl:map> </xsl:map-entry> </xsl:map> </xsl:map-entry> </xsl:map> </xsl:function> </xsl:stylesheet>
方案二:使用key函数结合变量文档
<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" exclude-result-prefixes="#all"> <!-- 将国家映射放在变量的文档节点中 --> <xsl:variable name="countryDoc"> <countries> <sf:country name="United States">US</sf:country> <sf:country name="Australia">AU</sf:country> <sf:country name="Germany">DE</sf:country> </countries> </xsl:variable> <!-- 针对变量文档定义key --> <xsl:key name="countryCodes" match="sf:country" use="@name" document="$countryDoc"/> <xsl:output method="json" indent="yes"/> <xsl:template match="/soapenv:Envelope"> <xsl:map> <xsl:map-entry key="'records'" select="array { soapenv:Body/queryMoreResponse/result/records ! mf:process-record(.) }"/> </xsl:map> </xsl:template> <xsl:function name="mf:process-record" as="map(*)"> <xsl:param name="record" as="element()"/> <xsl:variable name="countryName" select="string($record/sf:ConsumerCountry)"/> <!-- 指定document参数从变量文档中查找 --> <xsl:variable name="countryCode" select="key('countryCodes', $countryName, $countryDoc)/text()"/> <xsl:map> <xsl:map-entry key="'case'"> <xsl:map> <xsl:map-entry key="'safetyreport'"> <xsl:map> <xsl:map-entry key="'reportid'" select="string($record/sf:Id)"/> <xsl:map-entry key="'reportnumber'" select="string($record/sf:Number)"/> <xsl:map-entry key="'sourcecountry'" select="$countryCode"/> </xsl:map> </xsl:map-entry> </xsl:map> </xsl:map-entry> </xsl:map> </xsl:function> </xsl:stylesheet>
修正后输出
运行修正后的XSLT,将得到与预期一致的JSON输出:
{ "records": [ { "case": { "safetyreport": { "reportid": "6896ADSD888", "reportnumber": "123456666", "sourcecountry": "AU" } } }, { "case": { "safetyreport": { "reportid": "6896SDRGGG666", "reportnumber": "123456666", "sourcecountry": "US" } } } ] }
内容的提问来源于stack exchange,提问作者Rajat Kumar
相关产品推荐
相关产品推荐

