cancel
Showing results for 
Search instead for 
Did you mean: 

Complex Mapping Question

Former Member
0 Kudos

Hi,

i have a xml structur like that:

<root>
	<header>
		<nr>00010</nr>
	</header>
	<header>
		<nr>00102</nr>
	</header>
	<item>
		<nr>00010</nr>
		<value>20</value>
		<wight>60</wight>
	</item>
	<item>
		<nr>00102</nr>
		<value>30</value>
		<wight>30</wight>
	</item>
	<desc>
		<nr>00010</nr>
		<name>Meet</name>
	</desc>
	<desc>
		<nr>00102</nr>
		<name>Apple</name>
	</desc>
</root>

and want that it looks like that:

<root>
	<item>
		<nr>00010</nr>
		<name>Meet</name>
		<value>20</value>
		<wight>60</wight>
	</item>
	<item>
		<nr>00102</nr>
		<name>Apple</name>
		<value>30</value>
		<wight>30</wight>
	</item>
</root>

for each header tag in source message it must be a item tag in target message.

can i handle this with a XSLT Mapping ?

any Idea?

for me its looks very difficult to do that in a mapping.

Regards,

Robin

Accepted Solutions (1)

Accepted Solutions (1)

udo_martens
Active Contributor
0 Kudos

Hi Robin,

check this:

[code]

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:template match="/">

<root>

<xsl:for-each select="//header/nr">

<xsl:variable name="NR" select="."/>

<item>

<nr>

<xsl:value-of select="."/>

</nr>

<name>

<xsl:value-of select="../../desc/name[../nr=$NR]"/>

</name>

<value>

<xsl:value-of select="../../item/value[../nr=$NR]"/>

</value>

<wight>

<xsl:value-of select="../../item/wight[../nr=$NR]"/>

</wight>

</item>

</xsl:for-each>

</root>

</xsl:template>

</xsl:styleshee

[/code]

Regards,

Udo

Answers (2)

Answers (2)

Former Member
0 Kudos

I think you have your source xml wrong. Try getting your source xml structure to be something like below, the reason being you cannot do a loop (for-each) otherwise.

<?xml version="1.0" encoding="UTF-8" ?>

<root>

<data>

<header>

<nr>00010</nr>

</header>

<item>

<nr>00010</nr>

<value>20</value>

<wight>60</wight>

</item>

<desc>

<nr>00010</nr>

<name>Meet</name>

</desc>

</data>

<data>

<header>

<nr>00102</nr>

</header>

<item>

<nr>00102</nr>

<value>30</value>

<wight>30</wight>

</item>

<desc>

<nr>00102</nr>

<name>Apple</name>

</desc>

</data>

<data>

<header>

<nr>00102</nr>

</header>

<item>

<nr>00102</nr>

<value>30</value>

<wight>40</wight>

</item>

<desc>

<nr>00102</nr>

<name>Apple</name>

</desc>

</data>

</root>

Now if understood your requirement correctly, you need to club all nr together, and also aggregate (sum) their respective values and wight.

The XSLT to achieve that is as follows,

<?xml version='1.0'?>

<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:key name="nr" match="root/data" use="header/nr"/>

<xsl:template match="/">

<root>

<!-- iterate on each group -->

<xsl:for-each select="root/data[generate-id(.) = generate-id(key('nr', header/nr)[1]) ]">

<xsl:variable name="group" select="key('nr', header/nr)"/>

<xsl:variable name="nr">

<xsl:value-of select="header/nr"/>

</xsl:variable>

<xsl:variable name="name">

<xsl:value-of select="desc/name"/>

</xsl:variable>

<xsl:variable name="value">

<xsl:value-of select="sum($group/item/value)"/>

</xsl:variable>

<xsl:variable name="wight">

<xsl:value-of select="sum($group/item/wight)"/>

</xsl:variable>

<!-- Generate the final XML file -->

<item>

<nr>

<xsl:value-of select="$nr"/>

</nr>

<name>

<xsl:value-of select="$name"/>

</name>

<value>

<xsl:value-of select="$value"/>

</value>

<wight>

<xsl:value-of select="$wight"/>

</wight>

</item>

</xsl:for-each>

</root>

</xsl:template>

</xsl:stylesheet>

Please have a look at my code sample that i have submitted, which speaks of how to handle such a scenario.

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/xi/xi-code-samples... xslt mapping in sap xi, part i.pdf

cheers

Sameer

Former Member
0 Kudos

Hi Sameer,

my sample soure structure is like it is in many idocs.

But i dont want to post the idoc xml structure here

In my case it is the ARTMAS Idoc.

thats also why i can't change my source structure.

But you can loop to the structure when you do it like Udo says.

Or you can loop only at the header elements and call a template with parameter(the Number)in this loop to get the other values.

@ $

I try the Message Mapping when i have time for that

Thanks and Regards,

Robin

MichalKrawczyk
Active Contributor
0 Kudos