[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] convert xml to fixed length format

Subject: Re: [xsl] convert xml to fixed length format
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Thu, 19 Sep 2013 13:38:31 -0400


Using XSLT 2.0:

<xsl:template name="fixed-field">
  <xsl:param name="str" select="string(.)"/>
  <xsl:param name="length" as="xs:integer" select="10"/>
  <xsl:value-of select="substring($str,1,$length)"/>
  <xsl:for-each select="1 to ($length - string-length($str))">
    <xsl:text> </xsl:text>

<xsl:template match="id">
  <xsl:call-template name="fixed-field"/>

<xsl:template match="type">
  <xsl:call-template name="fixed-field">
    <xsl:with-param name="length" select="25"/>


Cheers, Wendell

Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables

On Thu, Sep 19, 2013 at 11:14 AM, a kusa <akusa8@xxxxxxxxx> wrote:
> Hello:
> Is it possible to convert an xml file into a fixed length format?
> For example, if I have an xml file as follows:
> <products>
> <product>
> <id>12345</id>
> <type>Toy</type>
> <description> This is a soft toy</description>
> </product>
> <product>
> <id>6789</id>
> <type>Car</type>
> <description> This is a car</description>
> </product>
> </products>
> Now, I want to convert this into a fixed length format based on
> specific string lengths. So if I defined id to be a fixed length of
> 10, type to be a fixed length of 25, and description to be a fixed
> length of 50, I want a text file with these values, and the rest of
> the spaces must be filled in such a way so as to meet the length
> criteria. That means, if id was only 5 digits long, there should be
> spaces for another 5 characters that total to 10, and the value for
> type must start on the 11th digit.
> Is this possible with XSLT?
> Any help is appreciated.

Current Thread