0

SAP is sending Empty Elements in XML Payload (IDOC). We need to remove the Empty Elements (<E1EDK02 SEGMENT="1"></E1EDK02>) in the IDOC below from the payload. I have used an XSLT script to remove these empty elements but still I see with the below XSLT, the Empty Elements are not removed but they are converted to <E1EDK02 SEGMENT="1"/>. Can anyone help us with the XSLT code which removes Empty.

IDOC From SAP

<?xml version="1.0" encoding="UTF-8"?><ORDERS05>
<IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
        <TABNAM>XXX</TABNAM>
        <MANDT>XXX</MANDT>
        <IDOCTYP>XXX</IDOCTYP>
    </EDI_DC40>
    <E1EDK02 SEGMENT="1">
        <QUALF>001</QUALF>
        <BELNR>TEST</BELNR>
        <DATUM>20210317</DATUM>
    </E1EDK02>
    <E1EDK02 SEGMENT="1"></E1EDK02>
    <E1EDK02 SEGMENT="1">
        <QUALF>002</QUALF>
        <BELNR>TEST</BELNR>
        <DATUM>20210317</DATUM>
    </E1EDK02>
    <E1EDK02 SEGMENT="1"></E1EDK02>
</IDOC></ORDERS05>

XSLT used

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" />
<xsl:template match="@*|node()">
    <xsl:if test=". != '' or ./@* != ''">
        <xsl:copy>
            <xsl:apply-templates  select="@*|node()"/>
        </xsl:copy>
    </xsl:if>
</xsl:template></xsl:stylesheet>

Expected Output

<?xml version="1.0" encoding="UTF-8"?><ORDERS05>
<IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
        <TABNAM>XXX</TABNAM>
        <MANDT>XXX</MANDT>
        <IDOCTYP>XXX</IDOCTYP>
    </EDI_DC40>
    <E1EDK02 SEGMENT="1">
        <QUALF>001</QUALF>
        <BELNR>TEST</BELNR>
        <DATUM>20210317</DATUM>
    </E1EDK02>
    <E1EDK02 SEGMENT="1">
        <QUALF>002</QUALF>
        <BELNR>TEST</BELNR>
        <DATUM>20210317</DATUM>
    </E1EDK02>
</IDOC></ORDERS05>

1 Answer 1

1

Your test:

<xsl:if test=". != '' or ./@* != ''">

passes any element that has a non-empty attribute. The element you wish to remove:

<E1EDK02 SEGMENT="1"></E1EDK02>

has a SEGMENT attribute and this attribute has a value - therefore, it passes your test.


If you want to remove elements that do not have child elements or text nodes, I would suggest you do:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- remove empty elements -->
<xsl:template match="*[not(node())]"/>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.