Search code examples
xsltsoapmapping

How do I filter XML nodes from SOAP response using XSLT


I have a situation where I need to filter certain XML nodes from SOAP response, I used XSLT mapping but it is not filtering specific xml nodes from SOAP Response, my requirement is to have xml node ns1:IntervalBlocks which has ns1:ReadingType ref="KWH_DEL" in the resulting XML, other ns1:IntervalBlocks should get removed

pls advise what is wrong in the logic

Here is my input 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 code

<?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>

I still get the full payload back in result without any filtration, I tried with some different logic but there is no difference in the result, seems it skips the entire logic,


Solution

  • If you want to remove ns1:IntervalBlocks then make your template match 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>
    

    This will remove the ns1:IntervalBlocks element that meets the condition. If you want to keep it and remove all others, then change it to:

    <xsl:template match="ns1:IntervalBlocks[ns1:ReadingType/@ref!='KWH_DEL']"/>