cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT code to delete node based on one field in the item node

Bhaven
Explorer
0 Kudos
295

Hello

Can someone please help in identifying the below issue in my xslt ? Req is to delete complete xml node based on one child node field.

XML input:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:root xmlns:ns1="urn:sap-com:document:sap:rfc:functions">
<IT_TRANSACTION>
<item>
<RETAILSTOREID>0000002620</RETAILSTOREID>
<TRANSTYPECODE>1001</TRANSTYPECODE>
<TRANSNUMBER>5</TRANSNUMBER>
<RETAILLINEITEM>
<item>
<ITEMIDQUALIFIER>1</ITEMIDQUALIFIER>
<ITEMID>4004845460629</ITEMID>
</item>
</RETAILLINEITEM>
<TAX>
<item>
<TAXTYPECODE>ZCO1</TAXTYPECODE>
<TAXAMOUNT>1.00</TAXAMOUNT>
</item>
</TAX>
</item>
<item>
<RETAILSTOREID>0000002621</RETAILSTOREID>
<TRANSTYPECODE/>
<TRANSNUMBER>6</TRANSNUMBER>
<RETAILLINEITEM>
<item>
<ITEMIDQUALIFIER>1</ITEMIDQUALIFIER>
<ITEMID>4004845460629</ITEMID>
</item>
</RETAILLINEITEM>
<TAX>
<item>
<TAXTYPECODE>ZCO1</TAXTYPECODE>
<TAXAMOUNT>1.00</TAXAMOUNT>
</item>
</TAX>
</item>
</IT_TRANSACTION>
</ns1:root>

 

XML output: 2nd item node should be completely deleted/rejected as TRANSTYPECODE is blank.

<?xml version="1.0" encoding="UTF-8"?>
<ns1:root xmlns:ns1="urn:sap-com:document:sap:rfc:functions">
<IT_TRANSACTION>
<item>
<RETAILSTOREID>0000002620</RETAILSTOREID>
<TRANSTYPECODE>1001</TRANSTYPECODE>
<TRANSNUMBER>5</TRANSNUMBER>
<RETAILLINEITEM>
<item>
<ITEMIDQUALIFIER>1</ITEMIDQUALIFIER>
<ITEMID>4004845460629</ITEMID>
</item>
</RETAILLINEITEM>
<TAX>
<item>
<TAXTYPECODE>ZCO1</TAXTYPECODE>
<TAXAMOUNT>1.00</TAXAMOUNT>
</item>
</TAX>
</item>
</IT_TRANSACTION>
</ns1:root>

XSL code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:ns1="urn:sap-com:document:sap:rfc:functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Match and output only the segments meeting the specified condition -->
<xsl:template match="ns1:item[TRANSTYPECODE = '']">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Suppress output of all other segments -->
<xsl:template match="//ns1:item"/>
</xsl:stylesheet>

View Entire Topic
Bhaven
Explorer
0 Kudos

Thanks, it works now.

 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item[TRANSTYPECODE = '']"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>