cancel
Showing results for 
Search instead for 
Did you mean: 

Remove particular null field before message mapping through XSLT or Groovy in SAP CPI

0 Kudos
1,340

Hi Experts,

Condition to remove particular null fields from source payload before mapping through Groovy or XSLT in SAP CPI.

For example,

if <Employee_address> and <Employee_PhoneNumber> are Null then remove these fields and send remaining fields in payload.

Please suggest me the code.

Thanks and Regards,

Surya.

Accepted Solutions (0)

Answers (3)

Answers (3)

Sriprasadsbhat
Active Contributor

Hello Surya,

Below will remove all the nodes which doesn't have any values.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
    <xsl:template match="*[not(child::node())]"/>
    <xsl:template match="@*|node()">
       <xsl:copy>
          <xsl:apply-templates />
       </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Regards,

Sriprasad Shivaram Bhat

0 Kudos

Thanks Sriprasad,

But is it way to remove particular field if it is null, instead of removing all null nodes?

Thanks & regards,

Surya.

arpit_rajgir4
Explorer
0 Kudos

Hello Sri,
I've a different requirement where I need to create the missing XML Node if they are missing. Can you suggest me how to achieve this using XSLT mapping?
arpit_rajgir4_0-1707717955077.png

Thanks in advance,
Arpit

RAAMS
Explorer
0 Kudos

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="node()|@*"> <xsl:if test="normalize-space(string(.)) != ''"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:if> </xsl:template> </xsl:stylesheet>

deepika11
Discoverer
0 Kudos

Thanks Prasad. All of your solutions helps a lot in our designs. I wanted to know if this is possible - in the xml structure if any one of the tag is empty, then remove the entire node. Here is the example

Input

<root>

<row>

<Order>12027</Order>

<OrderDescription>12027</OrderDescription>

<Item>A63466</Item>

<ItemDescription>Korean Sauce</ItemDescription>

<Component>A63466</Component>

<ComponentDescription>Korean Sauce</ComponentDescription>

<Plant>500</Plant> <WorkOrderStatus>3</WorkOrderStatus> </row> <row> <Order>12028</Order> <OrderDescription>12028</OrderDescription> <Item></Item> <ItemDescription/> <Component>A63466</Component> <ComponentDescription>Korean Sauce</ComponentDescription> <Plant>500</Plant> <WorkOrderStatus>3</WorkOrderStatus> </row> <row> <Order>12029</Order> <OrderDescription>12029</OrderDescription> <Item></Item> <ItemDescription/> <Component>A63466</Component> <ComponentDescription>Korean Sauce</ComponentDescription> <Plant>500</Plant> <WorkOrderStatus>3</WorkOrderStatus> </row> </root>

expected output - Since the second and third row have the item tag empty, we need to remove those 2 nodes and have the output as below

<root> <row> <Order>12027</Order> <OrderDescription>12027</OrderDescription> <Item>A63466</Item> <ItemDescription>Korean Sauce</ItemDescription> <Component>A63466</Component> <ComponentDescription>Korean Sauce</ComponentDescription> <Plant>500</Plant> <WorkOrderStatus>3</WorkOrderStatus> </row> </root>.

Please help us on this concern