Skip to Content
Technical Articles
Author's profile photo Deepak Jaiswal

Fixed Length File Generation Scenario Through SAP Cloud Platform Integration

Introduction:

One of the common requirements in SAP Cloud Platform Integration is to generate fixed length format output file for an end application. And this file will have different fields’ length based on application system specification. Generally, in SAP CPI message mapping can be used to produce the target xml using graphical mapping editor which in turn corresponds to the Custom XSD/Schema created for end/receiver application side similar to SAP PI/PO middleware. This final xml is now being passed through XML To CSV Converter module in SAP CPI to generate the file output having field separator as one of below options:

Comma(,),Colon(:) , Dot(.),Pipe(|),Semicolon(;),Space(),Tab(\t)

However, there is no functionality available for generating the fixed length format file using this convertor module in SAP CPI.

Here, I have explained one scenario where business is expecting fixed length format file with content of each field are left justified and remaining unused width of each field’s are padded with blank spaces to maintain every field’s start and end position in a text file output.

I have leveraged the use of a XSLT mapping to achieve this requirement.This XSLT mapping can be leveraged by developers to edit and modify and enhanced according to other business requirements as well.

Prerequisites: You should have CPI tenant access and have understanding on Integration flow development.

Design approach: To explain the scenario, I have used sample xml directly which is having header, detail and trailer records to avoid all the processing steps and directly jumping to the final xml format(used inside content modifier body of  SAP CPI iflow as test data xml ) to be converted into a fixed length file format using XSLT and place the file in SFTP folder using SAP CPI SFTP adapter.

 

Field specification are detailed below for the use case shown in this blog post:

Fields%20specification

Image 1: Fields specification

 

Now, Integration flow steps used for our use case demonstration purpose are mentioned below :

Sample%20SAP%20CPI%20Integration%20Flow

Image 2: Sample SAP CPI Integration Flow

 

Steps configured on Integration flow as below:

Step 1)

Content Modifier :To store the sample file xml to be passed to XSLT mapping in next step.

Content%20modifier%28Test%20xml%20Input%20provided%20for%20example%29

Image 3: Content modifier(Test xml Input provided for example)

Sample Input Xml for used in content modifier as shown below:

<Records>
	<Header>
		<HeaderIndicator>H</HeaderIndicator>
		<HeaderDate>20210405</HeaderDate>
		<HeaderFiller> </HeaderFiller>
	</Header>
	<Detail>
		<DetailIndicator>D</DetailIndicator>
		<DetailRecordDate>20210405</DetailRecordDate>
		<Name>Deepak Jaiswal</Name>
		<Phone>123457890</Phone>
	</Detail>
	<Detail>
		<DetailIndicator>D</DetailIndicator>
		<DetailRecordDate>20210405</DetailRecordDate>
		<Name>Rajesh Sharma</Name>
		<Phone>234578901</Phone>
	</Detail>
	<Detail>
		<DetailIndicator>D</DetailIndicator>
		<DetailRecordDate>20210405</DetailRecordDate>
		<Name>Lokesh Kumar</Name>
		<Phone>345789012</Phone>
	</Detail>
	<Trailer>
		<TrailerIndicator>T</TrailerIndicator>
		<TrailerDate>20210405</TrailerDate>
		<DetailRecordCount>0000000003</DetailRecordCount>
		<TrailerFiller> </TrailerFiller>
	</Trailer>
</Records>

Sample Input Xml( used in Content modifier step Screenshot)

 

Step 2) 

XSLT mapping : To convert input xml from step 1) to a fixed length format output file data:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output omit-xml-declaration="yes" indent="yes" method="text"/>
	<xsl:strip-space elements="*"/>
	
	<xsl:template match="node()|@*">
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="Header">
		<xsl:apply-templates />
		<xsl:text>
</xsl:text>
	</xsl:template>
	<xsl:template match="HeaderIndicator">
		<xsl:value-of
      select="substring(concat(., ' '), 1, 1)"/>
	</xsl:template>
	<xsl:template match="HeaderDate">
		<xsl:value-of
      select="substring(concat(., '          '), 1, 10)"/>
	</xsl:template>
	<xsl:template match="HeaderFiller">
		<xsl:value-of
      select="substring(concat(., '                                                                                                    '), 1, 100)"/>
	</xsl:template>
	
	<xsl:template match="Detail">
		<xsl:apply-templates />
		<xsl:text>
</xsl:text>
	</xsl:template>
	<xsl:template match="DetailIndicator">
		<xsl:value-of
      select="substring(concat(., ' '), 1, 1)"/>
	</xsl:template>
	<xsl:template match="DetailRecordDate">
		<xsl:value-of
      select="substring(concat(., '          '), 1, 10)"/>
	</xsl:template>
	<xsl:template match="Name">
		<xsl:value-of
      select="substring(concat(., '                                                  '), 1, 50)"/>
	</xsl:template>
	<xsl:template match="Phone">
		<xsl:value-of
      select="substring(concat(., '                                                  '), 1, 50)"/>
	</xsl:template>
	
	<xsl:template match="Trailer">
		<xsl:apply-templates />	
		<xsl:text>
</xsl:text>
	</xsl:template>
	<xsl:template match="TrailerIndicator">
		<xsl:value-of
      select="substring(concat(., ' '), 1, 1)"/>
	</xsl:template>
	<xsl:template match="TrailerDate">
		<xsl:value-of
      select="substring(concat(., '          '), 1, 10)"/>
	</xsl:template>
	<xsl:template match="DetailRecordCount">
		<xsl:value-of
      select="substring(concat(., '          '), 1, 10)"/>
	</xsl:template>
	<xsl:template match="TrailerFiller">
		<xsl:value-of
      select="substring(concat(., '                                                                                          '), 1, 90)"/>
	</xsl:template>
</xsl:stylesheet>

 

Here,  below example shows a part of xslt code for “Name” field which needs to be maintained similarly,for other fields based on their fixed field length value.

Example, for “Name” field, fixed field length value is 50 characters, so, we need to have 50 blank spaces and 50 as field length value maintained as per below highlighted part on XSLT code.

 

Image 4 : Portion of XSLT mapping to maintained correctly as per field length

 

Step 3):

SFTP adapter Processing: Final Fixed Length output file created from Step 2) XSLT code placed on the target directory in SFTP through SFTP adapter

Receiver%20SFTP%20Configuration%20%28example%29

Image 5: Receiver SFTP Configuration (example)

 

 

Fixed length file got generated at SFTP location configured as below:

File%20Created%20in%20SFTP%20Folder%20Path

Image 6: File Created in SFTP Folder Path

 

Final Output Fixed Length text file looks like below:

H20210405                                                                                                      
D20210405  Deepak Jaiswal                                    123457890                                         
D20210405  Rajesh Sharma                                     234578901                                         
D20210405  Lokesh Kumar                                      345789012                                         
T20210405  0000000003                                                                                          

 

Image 7 : File Output Notepad++ View

 

Conclusion:

In this blog post, I demonstrated  how to generate a file with fixed length format through SAP CPI integration flow using XSLT mapping.

Hope this blog post will help CPI developers to achieve similar functionality and this can be enhanced more to meet the technical business requirements.

 

Reference:

https://stackoverflow.com/questions/50332451/how-to-create-an-xsl-to-output-a-fixed-position-text-file

 

 

 

 

 

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Madhubala Chandran
      Madhubala Chandran

      Hi Deepak ,

      Thanks for sharing this knowledge and its very useful and informative.

      Thanks,

      Madhu

      Author's profile photo Rohit Saraf
      Rohit Saraf

      Nice blog Deepak

      Author's profile photo Daniel Graversen
      Daniel Graversen

      Nice.

      Yes XSLT does add some good options to convert flat files and also CSV which is also easy to setup.

      I do have to laugh at the part "is to generate fixed length format output file for an end application"

      Author's profile photo Abhijeet Baburao Thore
      Abhijeet Baburao Thore

      very well elaborated. Thanks for sharing this.

      Author's profile photo Vimal Pillai
      Vimal Pillai

      DJ, As always you have come up with another unique technique and this time it's in CPI.

      Great blog and information.

      Thanks for Sharing.