Skip to Content

Remove a Node from XML based on a Condition using XSLT

Hello,

I have a XML which looks like the one below

<?xml version="1.0" encoding="UTF-8"?>
<MT_SCN_TEST>
   <DataRow>
      <Row>
         <User>User1</User>
         <Segment01>
            <Amount>1000</Amount>
            <Currency>EUR</Currency>
         </Segment01>
      </Row>
      <Row>
         <User>User2</User>
         <Segment02>
            <CountryCode>11</CountryCode>
            <Region>AA</Region>
         </Segment02>
      </Row>
      <Row>
         <User>User3</User>
      </Row>
      <Row>
         <User>User4</User>
      </Row>
      <Row>
         <User>User5</User>
         <Segment01>
            <Amount>100</Amount>
            <Currency>EUR</Currency>
         </Segment01>
         <Segment02>
            <CountryCode>11</CountryCode>
            <Region>AA</Region>
         </Segment02>
      </Row>
   </DataRow>
</MT_SCN_TEST>

The requirement is to remove all the Row nodes where there is no instance of Segment01 or Segment02. So as per the above example, the expected output needs to be

<?xml version="1.0" encoding="UTF-8"?>
<MT_SCN_TEST>
   <DataRow>
      <Row>
         <User>User1</User>
         <Segment01>
            <Amount>1000</Amount>
            <Currency>EUR</Currency>
         </Segment01>
      </Row>
      <Row>
         <User>User2</User>
         <Segment02>
            <CountryCode>11</CountryCode>
            <Region>AA</Region>
         </Segment02>
      </Row>
      <Row>
         <User>User5</User>
         <Segment01>
            <Amount>100</Amount>
            <Currency>EUR</Currency>
         </Segment01>
         <Segment02>
            <CountryCode>11</CountryCode>
            <Region>AA</Region>
         </Segment02>
      </Row>
   </DataRow>
</MT_SCN_TEST>

Any inputs on how to achieve this using XSLT mapping?

I checked this example but it talks of removing empty nodes from the message

https://archive.sap.com/discussions/thread/3243372

Thank you!

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 16, 2018 at 08:03 AM

    Hi Abhishek

    Here's an XSLT solution:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output method="xml"/>
    
        <xsl:template match="/">
            <MT_SCN_TEST>
                <DataRow>
                    <xsl:apply-templates select="//Row[Segment01 or Segment02]"/>
                </DataRow>
            </MT_SCN_TEST>
        </xsl:template>
        
        <xsl:template match="node() | @*">
            <xsl:copy>
                <xsl:apply-templates select="node() | @*"/>
            </xsl:copy>
        </xsl:template>    
    
    </xsl:stylesheet>
    

    The last template copies any element, and the xsl:apply-templates instruction ensures that it is only applied to Row elements, that have either a Segment01 or a Segment02 child element.

    Regards,

    Morten

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 12, 2018 at 05:23 AM

    Hello Abhishek,

    Below should work thinking occurrence of DataRow is one.

    1) Add a Content Filter with below condition

    /MT_SCN_TEST/DataRow/Row[Segment02 or Segment01]

    2) Add content modifier to restore the original structure.

    <MT_SCN_TEST>
       <DataRow>
          ${in.body}
     </DataRow>
    </MT_SCN_TEST>
    

    Regards,

    Sriprasad Shivaram Bhat

    Add comment
    10|10000 characters needed characters exceeded