cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Mapping - Nested to flat file mapping with a slight twist

Former Member
0 Kudos

Hello,

I am new to SAP PI and mapping. I would be greatful if anyone could help me out a little here.

I have the following input structure sent from a web service (SOAP Sender):

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

<ns0:Input xmlns:ns0="urn:test">

  <Category>

  <ID>001</ID>

  <Book>

  <BookID>B1</BookID>

  <Name>Book1</Name>

  <Author>

  <FirstName>Alger</FirstName>

  <Lastname>Jones</Lastname>

  </Author>

  <Author>

  <FirstName>Sheryl</FirstName>

  <Lastname>Crow</Lastname>

  </Author>

  <Author>

  <FirstName>John</FirstName>

  <Lastname>Alistair</Lastname>

  </Author>

  </Book>

  <Book>

  <BookID>B2</BookID>

  <Name>Book2</Name>

  <Author>

  <FirstName>Martin</FirstName>

  <Lastname>Cook</Lastname>

  </Author>

  <Author>

  <FirstName>George</FirstName>

  <Lastname>Wood</Lastname>

  </Author>

  </Book>

  </Category>

</ns0:Input>

This has to be mapped to a .dat flat file as below:

AlgerJones

SherylCrow

JohnAlistair

B1Book1

MartinCookGeorgeWood

B2Book2

What should the output structure look like in PI and how would I possibly parse the inner node (Author) and then the outer node(Book)?

Can this be achieved using graphical mapping or would I need an XSLT? Any basic code example would be very helpful.

Also, the field ID="001" has to be appended to file name. How can this be done?

Regards,

Roger

Accepted Solutions (1)

Accepted Solutions (1)

former_member198445
Participant
0 Kudos

Roger,

One of the ways is to use XSLT mapping:

Try the following.


<?xml version="1.0"?>

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

  <xsl:template match="/">

  <Record>

  <xsl:apply-templates select="//Book"/>

  </Record>

  </xsl:template>

  <xsl:template match="//Book">

  <xsl:apply-templates select="Author"/>

  <Books>

  <BookID>

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

  </BookID>

  <Name>

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

  </Name>

  </Books>

  </xsl:template>

  <xsl:template match="Author">

  <Authors>

  <FirstName>

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

  </FirstName>

  <LastName>

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

  </LastName>

  </Authors>

  </xsl:template>

</xsl:stylesheet>

You will get a flat structure as expected.

Then, in the receiver File adapter, use File content conversion as explained here: Converting File Content in the Receiver Adapter

Use Authors, Books as recordset structures.

To add the field ID in file name, use Dynamic configuration.

I have had trouble in the past using a message mapping with UDF to update dynamic config alog with an XSLT Mapping. So, I recommend using an XSLT mapping itself to update dynamic config before the one above.

See Point 6) in the link here:  http://scn.sap.com/docs/DOC-46751 for a better understanding.

Here's what you would need anyway:


<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:map="java:java.util.Map"

xmlns:dyn="java:com.sap.aii.mapping.api.DynamicConfiguration"

xmlns:key="java:com.sap.aii.mapping.api.DynamicConfigurationKey" >

  <xsl:output indent="no"/>

  <xsl:param name="inputparam"/>

  <xsl:template match="/">

  <xsl:variable name="dynamic-conf" select="map:get($inputparam, 'DynamicConfiguration')"/>

  <xsl:variable name="dynamic-key" select="key:create('http://sap.com/xi/XI/System/File', 'FileName')"/>

  <xsl:variable name="dynamic-value" select="dyn:get($dynamic-conf, $dynamic-key)"/>

  <xsl:variable name="ID" select="//ID"/>

  <xsl:variable name="filename" select="concat($ID, '.dat')"/>

  <xsl:variable name="new-value" select="concat($dynamic-value, $filename)"/>

  <xsl:variable name="dummy" select="dyn:put($dynamic-conf, $dynamic-key, $new-value)"/>

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

  </xsl:template>

</xsl:stylesheet>

Let me know if this works.

Cheers,

Amith

Former Member
0 Kudos

Thanks Amith.

This worked perfectly for me..

Answers (3)

Answers (3)

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Roger,

Can you check by creating the target structure as suggested by Amit and FCC configuration to convert xml to flat file as per your requirement.

Regarding filename, you can use dynamic configuration to set the file name accordingly in your mapping itself.

Let me know if you get struck anywhere on this.

RaghuVamseedhar
Active Contributor
0 Kudos

Roger,

Please try this concept

Former Member
0 Kudos

Hi,

You have to use below concept.

So, first all create ur target structure which should look something like below and then use java code in the blog to remove BookNode from the output structure so that FCC can result the same output which you are looking for.

MT

  ---BookNode

               Authors

                     FirstName

                     LastName

               

               BookName

                        ID

                        Name

Thanks

Amit Srivastava