如何使用 XSLT 从 SOAP 响应中过滤 XML 节点

问题描述 投票:0回答:1

我有一种情况,我需要从 SOAP 响应中过滤某些 XML 节点,我使用了 XSLT 映射,但它没有从 SOAP 响应中过滤特定的 xml 节点,我的要求是具有 ns1:IntervalBlocks 的 xml 节点 ns1:ReadingType ref=生成的 XML 中的“KWH_DEL”,其他 ns1:IntervalBlocks 应被删除

请指出逻辑上有什么问题

这是我的输入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>

我仍然在没有任何过滤的情况下得到完整的有效负载,我尝试了一些不同的逻辑,但结果没有区别,似乎它跳过了整个逻辑,

xslt soap mapping
1个回答
0
投票

如果您想删除

ns1:IntervalBlocks
,请使您的模板匹配
ns1:IntervalBlocks

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns1="http://iec.ch/TC57/2011/MeterReadings#" >
<xsl:output method="xml" indent="yes"/>

<xsl:mode on-no-match="shallow-copy"/>  
      
<xsl:template match="ns1:IntervalBlocks[ns1:ReadingType/@ref='KWH_DEL']"/>  

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.