[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: convert xml to fixed length format

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

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:for-each>
</xsl:template>

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

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

Untested.

Cheers, Wendell

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


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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.