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

XSLT过滤SOAP响应指定XML节点失效,请求逻辑排查

XSLT过滤XML节点失效问题排查与修正

问题背景

  • 需求:从SOAP响应XML中,仅保留包含ns1:ReadingType ref="KWH_DEL"的ns1:IntervalBlocks节点,移除其他同类型节点
  • 当前异常:现有XSLT映射完全不生效,返回完整原始Payload,调整逻辑后仍无变化

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <ns0:Message xmlns:ns0="http://www.iec.ch/TC57/2010/schema/message">
            <ns0:Payload>
                <ns1:MeterReadings xmlns:ns1="http://iec.ch/TC57/2011/MeterReadings#">
                    <ns1:MeterReading>
                        <ns1:IntervalBlocks>
                            <ns1:IntervalReadings>
                                <ns1:source>DR</ns1:source>
                                <ns1:value>6.112E0</ns1:value>
                                <ns1:ReadingQualities>
                                    <ns1:ReadingQualityType ref="ACTUAL"/>
                                </ns1:ReadingQualities>
                                <ns1:timePeriod>
                                    <ns1:end>2024-03-15T00:00:00-07:00</ns1:end>
                                    <ns1:start>2024-03-15T00:00:00-07:00</ns1:start>
                                </ns1:timePeriod>
                            </ns1:IntervalReadings>
                            <ns1:IntervalReadings>
                                <ns1:source>DR</ns1:source>
                                <ns1:value>7.079E0</ns1:value>
                                <ns1:ReadingQualities>
                                    <ns1:ReadingQualityType ref="ACTUAL"/>
                                </ns1:ReadingQualities>
                                <ns1:timePeriod>
                                    <ns1:end>2024-03-16T00:00:00-07:00</ns1:end>
                                    <ns1:start>2024-03-16T00:00:00-07:00</ns1:start>
                                </ns1:timePeriod>
                            </ns1:IntervalReadings>
                            <ns1:ReadingType ref="KWH_DEL"/>
                        </ns1:IntervalBlocks>
                        <ns1:IntervalBlocks>
                            <ns1:IntervalReadings>
                                <ns1:source>DR</ns1:source>
                                <ns1:value>7.855E0</ns1:value>
                                <ns1:ReadingQualities>
                                    <ns1:ReadingQualityType ref="ACTUAL"/>
                                </ns1:ReadingQualities>
                                <ns1:timePeriod>
                                    <ns1:end>2024-03-17T00:00:00-07:00</ns1:end>
                                    <ns1:start>2024-03-17T00:00:00-07:00</ns1:start>
                                </ns1:timePeriod>
                            </ns1:IntervalReadings>
                            <ns1:ReadingType ref="KWH_NET"/>
                        </ns1:IntervalBlocks>
                        <ns1:IntervalBlocks>
                            <ns1:IntervalReadings>
                                <ns1:source>DR</ns1:source>
                                <ns1:value>0.0E0</ns1:value>
                                <ns1:ReadingQualities>
                                    <ns1:ReadingQualityType ref="INVALID"/>
                                </ns1:ReadingQualities>
                                <ns1:timePeriod>
                                    <ns1:end>2024-03-17T00:00:00-07:00</ns1:end>
                                    <ns1:start>2024-03-17T00:00:00-07:00</ns1:start>
                                </ns1:timePeriod>
                            </ns1:IntervalReadings>
                            <ns1:ReadingType ref="KWH_REC"/>
                        </ns1:IntervalBlocks>
                    </ns1:MeterReading>
                </ns1:MeterReadings>
            </ns0:Payload>
        </ns0:Message>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

现有失效的XSLT代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.iec.ch/TC57/2010/schema/message" xmlns:ns1="http://iec.ch/TC57/2011/MeterReadings#" xmlns:n0="http://sap.com/xi/SFIHCM01" version="3.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:mode on-no-match="shallow-copy"/>
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="ns1:ReadingType[@ref = 'KWH_DEL' ]" />
</xsl:stylesheet>

错误原因分析

  1. 逻辑完全反向:现有模板是移除ns1:ReadingType[@ref='KWH_DEL']节点,和需求(保留包含该节点的IntervalBlocks)完全相反,相当于做了反操作。
  2. 模板冗余冲突:XSLT 3.0中xsl:mode on-no-match="shallow-copy"已经实现了默认拷贝所有未匹配节点的功能,再手动定义@* | node()的拷贝模板属于冗余,可能导致模板优先级冲突,自定义模板无法正常执行。

修正后的XSLT方案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                xmlns:ns0="http://www.iec.ch/TC57/2010/schema/message" 
                xmlns:ns1="http://iec.ch/TC57/2011/MeterReadings#" 
                xmlns:n0="http://sap.com/xi/SFIHCM01" 
                version="3.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:mode on-no-match="shallow-copy"/>
    
    <!-- 移除所有不包含目标ReadingType的IntervalBlocks节点 -->
    <xsl:template match="ns1:IntervalBlocks[not(ns1:ReadingType[@ref='KWH_DEL'])]"/>
</xsl:stylesheet>

代码说明

  • xsl:mode on-no-match="shallow-copy":默认拷贝所有未被自定义模板匹配的节点,无需手动写通用拷贝模板,简化代码。
  • 自定义模板匹配不包含ns1:ReadingType[@ref='KWH_DEL']的IntervalBlocks节点,空模板表示直接移除这些节点;符合条件的IntervalBlocks会被默认的shallow-copy规则完整保留。

内容的提问来源于stack exchange,提问作者Sam K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 02:39:51