cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI XSLT - delete empty nodes

DLange
Participant
0 Kudos
1,572

Hi Community,

I guess someone have already an similar use case / requirement and can easily help. I have following payload in XML:

<customerlist>
   <data>
     <name1>Meier</name1>
     <name2>Hans</name2>
   </data>
   <data>
     <name1><name1>
     <name2>Fischer</name2>
   </data>
   <data>
     <name1>Müller<name1>
     <name2></name2>
   </data>
   <data/>
   </data/>
</customerlist>

My goal is to delete only the complete empty "data" elements of the list. Empty "name1" or "name2" should be stay untouched.

Thanks

Best regards

DL

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
SAP Mentor
SAP Mentor

Hi Dominik

You can solve this using the technique I described in this blog post: Thinking in XSLT: Filtering XML elements

In your specific case, the stylesheet would look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="data[not(*)]"/>
</xsl:stylesheet>

Regards,

Morten

DLange
Participant
0 Kudos

Hi 7a519509aed84a2c9e6f627841825b5a,

is your provided example already a valid XSL because I am getting an error if I want to test the "transformation" of the XML payload with your example in an online tool.

Thanks

Best regards

DL

MortenWittrock
SAP Mentor
SAP Mentor
0 Kudos

Hi dlange_87

Yes, it's a valid stylesheet. Your input XML, however, is not valid, which is why your test fails. Here's a cleaned up version of your XML without the errors:

<?xml version="1.0" encoding="UTF-8"?>
<customerlist>
<data>
<name1>Meier</name1>
<name2>Hans</name2>
</data>
<data>
<name1/>
<name2>Fischer</name2>
</data>
<data>
<name1>Müller</name1>
<name2/>
</data>
<data/>
</customerlist>

Regards,

Morten

DLange
Participant
0 Kudos

Hi 7a519509aed84a2c9e6f627841825b5a,

thanks for your feedback. I was able to solve the issue. Empty "data" elements will be deleted and other XML elements which are empty stay untouched.

Thanks

Best regards

DL

MortenWittrock
SAP Mentor
SAP Mentor

Hi dlange_87

If my answer solved or helped solve the problem, please keep in mind to accept/upvote.

Regards,

Morten

Answers (1)

Answers (1)

Subhadeep
Explorer
0 Kudos

Hello Morten,

Hope you are doing fine. I used your XSLT script in removing the empty nodes in XML but I am unable to remove empty segments.

For example: This is the XML

<?xml version="1.0" encoding ="utf-8"?>
<Record>
<Employee>
<Name>Robin</Name>
<Designation>Engineer</Designation>
</Employee>
<Employee>
<Name>Samuel</Name>
<Designation>Manager</Designation>
</Employee>
</Record>

Now when I remove Samuel and Manager, I am getting the output as:

<?xml version="1.0" encoding ="utf-8"?>
<Record>
<Employee>
<Name>Robin</Name>
<Designation>Engineer</Designation>
</Employee>
<Employee>

</Employee>
</Record>

However , I want no segment if there are empty nodes. Below is the expected output:

<?xml version="1.0" encoding ="utf-8"?>
<Record>
<Employee>
<Name>Robin</Name>
<Designation>Engineer</Designation>
</Employee>
</Record>

Please help here if we can achieve this output.

regards,

Subhadeep