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

编写XSLT样式表从SOAP XML提取指定数据并转换为目标XML

正确的XSLT转换实现方案

示例输入SOAP XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
  <soapenv:Body>
    <queryResponse>
      <result>
        <records xsi:type="sf:Case" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <sf:Id>CASE-001</sf:Id>
          <sf:PolicyNumber>POL-12345</sf:PolicyNumber>
          <sf:Case_Responses_GCC__r>
            <records xsi:type="sf:Case_Response__c">
              <sf:Question__c>您的保单生效日期是?</sf:Question__c>
              <sf:Answer__c>2023-01-01</sf:Answer__c>
            </records>
            <records xsi:type="sf:Case_Response__c">
              <sf:Question__c>您的索赔金额是多少?</sf:Question__c>
              <sf:Answer__c>5000元</sf:Answer__c>
            </records>
          </sf:Case_Responses_GCC__r>
        </records>
        <records xsi:type="sf:Case" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <sf:Id>CASE-002</sf:Id>
          <sf:PolicyNumber>POL-67890</sf:PolicyNumber>
          <sf:Case_Responses_GCC__r>
            <records xsi:type="sf:Case_Response__c">
              <sf:Question__c>您的出险地点是?</sf:Question__c>
              <sf:Answer__c>北京市朝阳区</sf:Answer__c>
            </records>
          </sf:Case_Responses_GCC__r>
        </records>
      </result>
    </queryResponse>
  </soapenv:Body>
</soapenv:Envelope>

目标输出XML结构

<Cases>
  <Case>
    <Id>CASE-001</Id>
    <PolicyNumber>POL-12345</PolicyNumber>
    <summary>您的保单生效日期是?:2023-01-01;您的索赔金额是多少?:5000元</summary>
  </Case>
  <Case>
    <Id>CASE-002</Id>
    <PolicyNumber>POL-67890</PolicyNumber>
    <summary>您的出险地点是?:北京市朝阳区</summary>
  </Case>
</Cases>

实现XSLT样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:sf="urn:sobject.enterprise.soap.sforce.com"
                exclude-result-prefixes="sf">

  <!-- 匹配根节点,生成输出根元素 -->
  <xsl:template match="/">
    <Cases>
      <xsl:apply-templates select="//sf:Case"/>
    </Cases>
  </xsl:template>

  <!-- 处理每条sf:Case记录 -->
  <xsl:template match="sf:Case">
    <Case>
      <Id>
        <xsl:value-of select="sf:Id"/>
      </Id>
      <PolicyNumber>
        <xsl:value-of select="sf:PolicyNumber"/>
      </PolicyNumber>
      <summary>
        <!-- 遍历所有问答子记录并拼接 -->
        <xsl:for-each select="sf:Case_Responses_GCC__r/sf:records">
          <xsl:if test="position() > 1">;</xsl:if>
          <xsl:value-of select="sf:Question__c"/>:<xsl:value-of select="sf:Answer__c"/>
        </xsl:for-each>
      </summary>
    </Case>
  </xsl:template>

  <!-- 忽略原XML中无关的文本和属性节点 -->
  <xsl:template match="text()|@*"/>
</xsl:stylesheet>

关键实现说明

  • 命名空间处理:声明原XML中的sf命名空间,并用exclude-result-prefixes避免命名空间污染输出结果。
  • 节点定位:通过//sf:Case精准定位所有案件记录,确保每条记录都被处理。
  • 问答整合逻辑:用<xsl:for-each>遍历sf:Case_Responses_GCC__r下的子记录,通过position() > 1控制分隔符的添加,避免开头出现多余符号。
  • 冗余内容过滤:最后一个模板匹配所有文本和属性节点,确保原XML中无关内容不会被输出。

内容的提问来源于stack exchange,提问作者Rajat Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 18:05:16