You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT提取Salesforce SOAP响应中sf:Address内的子标签值?

解决Salesforce SOAP响应中提取sf:Address子元素值的问题

你遇到的问题核心是命名空间未正确声明与使用,Salesforce的SOAP响应里所有带sf:前缀的元素都属于特定命名空间,XSLT必须先识别这个命名空间才能正确匹配节点。

关键步骤

  1. 确认SOAP响应中的命名空间
    先查看你的SOAP XML根元素,sf前缀对应的命名空间通常是类似这样的:

    xmlns:sf="urn:sobject.enterprise.soap.sforce.com"
    

    (也可能是http://soap.sforce.com/2006/04/metadata,以实际XML中的声明为准)

  2. 在XSLT中声明同名命名空间
    在XSLT的<xsl:stylesheet>根标签里,添加对应命名空间的声明,比如:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
    
  3. 修正XPath表达式
    注意city、country这些子元素同样属于sf命名空间,所以XPath里必须加上sf:前缀,正确写法是:

    <xsl:value-of select="//sf:Address/sf:city"/>
    <xsl:value-of select="//sf:Address/sf:country"/>
    

完整示例

示例SOAP响应片段

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
  <soapenv:Body>
    <queryResponse>
      <result>
        <records>
          <sf:Address>
            <sf:city>Hyderabad</sf:city>
            <sf:country>India</sf:country>
          </sf:Address>
        </records>
      </result>
    </queryResponse>
  </soapenv:Body>
</soapenv:Envelope>

对应的XSLT代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
  <xsl:template match="/">
    <extracted-data>
      <city><xsl:value-of select="//sf:Address/sf:city"/></city>
      <country><xsl:value-of select="//sf:Address/sf:country"/></country>
    </extracted-data>
  </xsl:template>
</xsl:stylesheet>

常见坑点

  • 漏加命名空间声明:XSLT无法识别sf:前缀,导致XPath匹配失败
  • 子元素未加前缀:只写city而不是sf:city,忽略了子元素也属于同一命名空间
  • 命名空间URI写错:必须和SOAP响应里的URI完全一致,大小写、特殊字符都不能错

内容的提问来源于stack exchange,提问作者Aman Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:12:44