cancel
Showing results for 
Search instead for 
Did you mean: 

How to remove Empty Nodes and Segments in XML within SAP CPI

Subhadeep
Explorer
0 Kudos
2,050
Hello SAP Experts,Hope you are doing well.I have a requirement of removing Empty nodes and Segments for an XML in SAP CPI.I am using this XSLT script in removing the empty nodes in XML but I am unable to remove empty segments.

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

This is the XML input:

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

This is the present output, I am getting using the above script when I remove Samuel and Manager in the input xml:

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

Accepted Solutions (1)

Accepted Solutions (1)

Ryan-Crosby
Active Contributor

Hi Subhadeep,

The following should work. I already posted once and had to delete it because the original one only worked for Employee nodes that had Name/Designation nodes without text, but this version works when Employee contains no nodes either.

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" encoding="utf-8" indent="yes"/>
	<xsl:template match="node()[child::*[not(text())]]|node()[not(child::*) and contains(text(), ' ')]"/>
        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
        </xsl:template>
</xsl:stylesheet>

Regards,

Ryan Crosby

tanweer_ahmad75
Explorer
0 Kudos

HI Ryan
How can we remove child node in XML only if its sub-child is empty?
for eg , Input
<Record>

<Employee>

<Name>Robin</Name>

<Designation></Designation>

</Employee>

<Employee>

<Name></Name>

<Designation></Designation>

</Employee>

</Record>

Output:
<Record>

<Employee>

<Name>Robin</Name>

<Designation></Designation>

</Employee>

</Record>

Answers (2)

Answers (2)

Subhadeep
Explorer
0 Kudos

Hello Ryan,

I need your help in slight modification of the XSLT.

It should work with empty values and not blank values in the nodes:

Currently it is working with

<Name> </Name> ( A blank character within the tags <Name>)

Our functionality is to make it work with empty values:

<Name></Name> ( No blank spaces)

Can you append the code for this requirement, please.

regards,

Subhadeep

Ryan-Crosby
Active Contributor
0 Kudos

You can try the extra following match to see if it works with spaces.

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" encoding="utf-8" indent="yes"/>
	<xsl:template match="node()[child::*[not(text())]]|node()[child::*[contains(text(), ' ')]]|node()[not(child::*) and contains(text(), ' ')]"/>
        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
        </xsl:template>
</xsl:stylesheet>
Subhadeep
Explorer
0 Kudos

Many thanks Ryan, it worked!

Ryan-Crosby
Active Contributor

Cheers!

Subhadeep
Explorer
0 Kudos

Thanks Ryan, It will be very helpful to modify the existing code with the new observations. I have posted it in the section below. Please have a look.