cancel
Showing results for 
Search instead for 
Did you mean: 

Remove a Child Node if empty from XML based on a Condition using XSLT

test_user1512
Explorer
0 Kudos

Hello Experts,

I want to remove the Parent node <A_BusinessPartnerAddressType> if the child node <A_BusinessPartnerRoleType> does not exisit.

So in the below XML it should delete the <BusinessPartner>22222</BusinessPartner> since <A_BusinessPartnerRoleType> does not exist. How should I achieve this using XSLT mapping.

I tried few approaches from sap forumn but its not working as expected.

Regards

Umesh

Accepted Solutions (0)

Answers (3)

Answers (3)

daviddasilva
Active Contributor
0 Kudos

Hello,

The suggestions here are great!

You could also do this with a message mapping step if the need arises.

You may need to play around with the contexts or use a CollapseContexts followed by a SplitByValue function if there are many A_BusinessPartnerRoleType nodes but you could use standard functions to achieve this.

A_BusinessPartnerRoleType > CollapseContexts > SplitByValue> Exists > CreateIf > A_BusinessPartnerAddressType

Then the node will only be created if Partner Role Type exists.

Kind regards,

David

MortenWittrock
Active Contributor
0 Kudos

Hi Umesh

Please remember to include the source XML as text, not a screenshot. That makes it easy to test a solution.

So I haven't tested this, but the filtering approach is the same as I described in this blog post. Here's the stylesheet:

<?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="A_BusinessPartnerAddressType[not(A_BusinessPartnerRoleType)]" />
</xsl:stylesheet>
Regards,Morten
PPiriou
Explorer
0 Kudos

You probably need to compare the value of one child element to make it work, like this example below where I compare the BusinessPartner value.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">

<xsl:copy>

<xsl:apply-templates select="node()|@*"/>

</xsl:copy>

</xsl:template>

<xsl:template match="A_BusinessPartnerAddressType[not(@BusinessPartner < '99999')]"/>

</xsl:stylesheet>

Regards,

Patrick Piriou