Convert XML to Fixed Length

Questions about XML that are not covered by the other forums should go here.
awolff01
Posts: 4
Joined: Thu May 18, 2006 7:59 pm

Convert XML to Fixed Length

Post by awolff01 »

Hello.

Wondering if it's possible to convert an XML document to a fixed length file via an XSLT. Any examples out there?


Do I need a specific parser such Saxon8 or is a 1.0 Processor good enough?


Thank You.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

You did not specify how the fixed length output file depends on the (variable length ?) input XML file. What should the transformation do ? Without knowing more about this it is impossible to help you. Can you post small samples of the input XML file and the output file ?


Regards,
Sorin
Gayathri
Posts: 1
Joined: Tue Dec 05, 2006 8:37 am

Post by Gayathri »

sorin wrote:Hello,

You did not specify how the fixed length output file depends on the (variable length ?) input XML file. What should the transformation do ? Without knowing more about this it is impossible to help you. Can you post small samples of the input XML file and the output file ?


Regards,
Sorin
Hi sorin,

you can try this out for transfrming a XML file to a fixed length file.

EX:if u want to convert EMP_NO= "12 " to make it to the length of 10.
ur output will be "0000000012".

Here is the code.

Use this template

<xsl:template name="append-pad-left">
<xsl:param name="padChar"> </xsl:param>
<xsl:param name="padVar"/>
<xsl:param name="length"/>
<xsl:choose>
<xsl:when test="string-length($padVar) < $length">
<xsl:call-template name="append-pad-left">
<xsl:with-param name="padChar" select="$padChar"/>
<xsl:with-param name="padVar" select="concat($padVar,$padChar)"/>
<xsl:with-param name="length" select="$length"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($padVar,1,$length)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

and call this template wherever u need.

<xsl:call-template name="append-pad-left">
<xsl:with-param name="padChar" select="' '"></xsl:with-param>
<xsl:with-param name="padVar" select="EMP_NO"></xsl:with-param>
<xsl:with-param name="length" select="10"/>
</xsl:call-template>



Thanks
Gaya
Post Reply