cancel
Showing results for 
Search instead for 
Did you mean: 

Replicate and modify xml node using XSLT

Bhaven
Explorer
0 Kudos
177

Hello

XSL gurus, will appreciate if someone can guide me for below issue in XSLT (3.0 version is ok).

Req is to replicate <RETAILLINEITEM/item> node and also modify some values based on some condition.

Condition/rule to replicate and modify is

TRANSTYPECODE = 1003 and RETAILTYPECODE = 2003

<SALESAMOUNT> field to be multiplied by -1.

Input payload:

<ns1:root xmlns:ns1="urn:sap-com:document:sap:rfc:functions">
<IT_TRANSACTION>
<item>
<RETAILSTOREID>01962</RETAILSTOREID>
<TRANSTYPECODE>1003</TRANSTYPECODE>
<RETAILLINEITEM>
<item>
<RETAILNUMBER>0</RETAILNUMBER>
<RETAILTYPECODE>2003</RETAILTYPECODE>
<ITEMIDQUALIFIER>1</ITEMIDQUALIFIER>
<RETAILQUANTITY>1</RETAILQUANTITY>
<SALESUOM>PCE</SALESUOM>
<SALESAMOUNT>4.44</SALESAMOUNT>
</item>
</RETAILLINEITEM>
<TAX>
<item>
<TAXNUMBER>1</TAXNUMBER>
<TAXTYPECODE>ZST1</TAXTYPECODE>
<TAXAMOUNT>0.00</TAXAMOUNT>
</item>
</TAX>
</item>
</IT_TRANSACTION>
</ns1:root>

<RETAILLINEITEM/item> should be copied and also amount values to be multiplied by -1

Output payload:

<ns1:root xmlns:ns1="urn:sap-com:document:sap:rfc:functions">
<IT_TRANSACTION>
<item>
<RETAILSTOREID>01962</RETAILSTOREID>
<TRANSTYPECODE>1003</TRANSTYPECODE>
<RETAILLINEITEM>
<item>
<RETAILNUMBER>0</RETAILNUMBER>
<RETAILTYPECODE>2003</RETAILTYPECODE>
<ITEMIDQUALIFIER>1</ITEMIDQUALIFIER>
<RETAILQUANTITY>1</RETAILQUANTITY>
<SALESUOM>PCE</SALESUOM>
<SALESAMOUNT>4.44</SALESAMOUNT>
</item>
<!-- Below item node should be copied from above and <salesamount> should be multiplied by -1 -->
<item>
<RETAILNUMBER>0</RETAILNUMBER>
<RETAILTYPECODE>2003</RETAILTYPECODE>
<ITEMIDQUALIFIER>1</ITEMIDQUALIFIER>
<RETAILQUANTITY>1</RETAILQUANTITY>
<SALESUOM>PCE</SALESUOM>
<SALESAMOUNT>-4.44</SALESAMOUNT> <!-- Negative amt -->
</item>
</RETAILLINEITEM>
<TAX>
<item>
<TAXNUMBER>1</TAXNUMBER>
<TAXTYPECODE>ZST1</TAXTYPECODE>
<TAXAMOUNT>0.00</TAXAMOUNT>
</item>
</TAX>
</item>
</IT_TRANSACTION>
</ns1:root>

View Entire Topic
jamie_cawley
Product and Topic Expert
Product and Topic Expert
0 Kudos

How about

<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
	<xsl:template match="@* | node()" mode="#all">
		<xsl:copy>
			<xsl:apply-templates select="@* | node()" mode="#current"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="RETAILLINEITEM/item">
		<xsl:copy-of select="."/>
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="RETAILLINEITEM/item/SALESAMOUNT" >
		<xsl:copy>
			<xsl:value-of select=". * -1"/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>

Regards,

Jamie

Bhaven
Explorer
0 Kudos
Thanks Jamie - How does xsl know that is has to only change the copied amount by *-1 and not the 1st original amount?
jamie_cawley
Product and Topic Expert
Product and Topic Expert
I would suggest reading xslt documentation to understand. There's a number of ways to solve every scenario.