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

Creating Fixed-Width Text Data

Subject: Creating Fixed-Width Text Data
From: "Michael Peet" <mjpeet@xxxxxxxxxxx>
Date: Tue, 14 Jan 2003 09:02:16 -0500
xsl fixed width
Hi All,

I have the need to create a fixed-width text record. Fields within the record can be either numeric or character. Numeric fields are zero-padded, right justified. Character fields are space-padded, left justified. A simplified example would look something like:

000123hello 00

I've developed a stylesheet with recursive templates for padding the field data, but was wondering if anyone knows of a slicker way to do it. Code follows, sorry for the long post!

XML:

<fields>
 <field>
   <name>foo</name>
   <type>N</type>
   <length>6</length>
   <value>123</value>
 </field>
 <field>
   <name>bar</name>
   <type>C</type>
   <length>12</length>
   <value>hello</value>
 </field>
 <field>
   <name>baz</name>
   <type>N</type>
   <length>2</length>
   <value/>
 </field>
</fields>

XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="text"/>

 <xsl:template match="/">
   <xsl:apply-templates select="fields"/>
 </xsl:template>

 <xsl:template match="fields">
   <xsl:apply-templates select="field"/>
 </xsl:template>

 <xsl:template match="field[type = 'N']">
   <xsl:call-template name="pad-number">
     <xsl:with-param name="length" select="length"/>
     <xsl:with-param name="value" select="value"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template match="field[type = 'C']">
   <xsl:call-template name="pad-string">
     <xsl:with-param name="length" select="length"/>
     <xsl:with-param name="value" select="value"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="pad-string">
   <xsl:param name="length"/>
   <xsl:param name="value"/>
   <xsl:param name="pos" select="string-length($value)"/>
   <xsl:choose>
     <xsl:when test="$pos &lt; $length">
       <xsl:call-template name="pad-string">
         <xsl:with-param name="length" select="$length"/>
         <xsl:with-param name="value" select="concat($value, ' ')"/>
         <xsl:with-param name="pos" select="$pos + 1"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$value"/>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

 <xsl:template name="pad-number">
   <xsl:param name="length"/>
   <xsl:param name="value"/>
   <xsl:param name="pos" select="string-length($value)"/>
   <xsl:choose>
     <xsl:when test="$pos &lt; $length">
       <xsl:call-template name="pad-number">
         <xsl:with-param name="length" select="$length"/>
         <xsl:with-param name="value" select="concat('0', $value)"/>
         <xsl:with-param name="pos" select="$pos + 1"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$value"/>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

</xsl:stylesheet>

OUTPUT:

000123hello 00

Best Regards,

Mike


_________________________________________________________________
Help STOP SPAM: Try the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



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.