Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
lalit2054_46
Participant

Introduction


It is quite common to generate fixed length files in different integration scenarios. SAP Cloud Integration does not provide any standard way of generating it, but the best thing about it is that it supports XSLT. XSL (eXtensible Stylesheet Language) is a styling language for XML but you can use it to generate any sort of output. XSL 2.0 supports creating functions which can be reused at different places just like a normal function.Normally the procedure to generate fixed length data is to use concat function and append/prepend  some spaces based on the size of the input text. But then this is quite manual work.

Following steps are to be done in order to generate fixed line files -

Output


The default output format of xslt is XML, so we should change it to text by adding below output tag under stylesheet element.
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

Namespace


Next we need to add a namespace xcpi to be used for the functions that we will be defining.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xcpi="http://www.xcpi.com/xfunction" xmlns:xs="http://www.w3.org/2001/XMLSchema">

New Line


We generally add new lines to the fixed length files. In order to add new line character we will be defining newline variable (remove space in select attr)
<xsl:variable name="newline" select="'&#xa ;'"/>

Functions


We should define three functions

xcpi:duplicate


This function will be used to repeat the string and takes two arguments

  1. input char - char like space or 0

  2. count - number of times you want to duplicate/repeat the characters


<xsl:function name="xcpi:dup">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:sequence select="string-join(for $i in 1 to $count return $input,'')"/>
</xsl:function>


xcpi:padLeft


This function will be used to prefix the string with the character specified and takes three arguments

  1. value - input string

  2. width - size of the field

  3. paddingChar - char to be used to pad remaining spaces



<xsl:function name="xcpi:padLeft">
<xsl:param name="value" as="xs:string"/>
<xsl:param name="width" as="xs:integer"/>
<xsl:param name="paddingChar" as="xs:string"/>

<xsl:variable name="output" select="substring($value,1,$width)"/>
<xsl:value-of select="xcpi:dup($paddingChar, $width - string-length($output))"/>
<xsl:value-of select="$output"/>
</xsl:function>

 

xcpi:padRight


This function will be used to postfix the string with the character specified and takes three arguments

  1. value - input string

  2. width - size of the field

  3. paddingChar - char to be used to pad remaining spaces.


<xsl:function name="xcpi:padRight">
<xsl:param name="value" />
<xsl:param name="width" as="xs:integer"/>
<xsl:param name="paddingChar" as="xs:string"/>

<xsl:variable name="output" select="substring($value,1,$width)"/>
<xsl:value-of select="$output"/>
<xsl:value-of select="xcpi:dup($paddingChar, $width - string-length($output))"/>
</xsl:function>

Usage


For e.g. we would like to generate a fixed size field with size 32 and pad left with space -
<xsl:value-of select="xcpi:padLeft($ProfitCenter, 32, ' ')"/>

For pad Right -
<xsl:value-of select="xcpi:padRight($ProfitCenter, 32, ' ')"/>


Complete Example


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xcpi="http://www.xcpi.com/xfunction" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text" encoding="UTF-8" indent="yes"/>
<xsl:variable name="newline" select="'&#xa ;'"/>
<xsl:template match="/">
<xsl:for-each select="/Articles">
<xsl:value-of select="xcpi:padLeft(ArticleName, 10, ' ')"/>
<xsl:value-of select="xcpi:padRight(ArticleNumber, 15, '0')"/>
<!-- New line -->
<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:template>

<!--Utility functions-->
<xsl:function name="xcpi:padRight">
<xsl:param name="value" as="xs:string"/>
<xsl:param name="width" as="xs:integer"/>
<xsl:param name="paddingChar" as="xs:string"/>

<xsl:variable name="output" select="substring($value,1,$width)"/>
<xsl:value-of select="$output"/>
<xsl:value-of select="xcpi:dup($paddingChar, $width - string-length($output))"/>
</xsl:function>
<xsl:function name="xcpi:padLeft">
<xsl:param name="value" as="xs:string"/>
<xsl:param name="width" as="xs:integer"/>
<xsl:param name="paddingChar" as="xs:string"/>

<xsl:variable name="output" select="substring($value,1,$width)"/>
<xsl:value-of select="xcpi:dup($paddingChar, $width - string-length($output))"/>
<xsl:value-of select="$output"/>
</xsl:function>
<xsl:function name="xcpi:dup">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:sequence select="string-join(for $i in 1 to $count return $input,'')"/>
</xsl:function>
</xsl:stylesheet>

Summary


I have shown you how to generate fixed size fields easily using functions. It is quite easy to maintain the xslt as it is easily modifiable with the help of functions. Kindly comment below and let me know we can improve this.
Labels in this area