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

Re: Fixed width text output

Subject: Re: Fixed width text output
From: "Mitch C. Amiano" <Mitch.Amiano@xxxxxxxxxxx>
Date: Wed, 03 Oct 2001 13:22:48 -0400
perl fixed width output
Padding To A Fixed Field Size 

Assuming an input XML document "somedoc.xml" that looks like this: 

<somedoc someattribute="xyz">
   <name>ABCD</name>
</somdoc>

We want to create a fixed position ASCII output that looks like this: 

      ABCD     xyz

In other words, we want to left-pad the two fields with whitespace
characters, and ensure that they are individually padded to a specified
fixed length. 

To do this, you can call a recursive template, or use a variable
containing a lot of spaces using substring. Here is an example of the
recursive template:

<?xml version="1.0"?>
<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0"
>
<xsl:preserve-space elements="*"/>
<xsl:output method="text"/>

<xsl:template match="/somedoc">

<xsl:call-template name="rightpad"><xsl:with-param name="fieldvalue"
select="string(./name)"/><xsl:with-param name="fieldsize"
select="number(10)"/></xsl:call-template><xsl:text> </xsl:text>
<xsl:call-template name="rightpad"><xsl:with-param name="fieldvalue"
select="string(@someattribute)"/><xsl:with-param name="fieldsize"
select="number(8)"/></xsl:call-template><xsl:text> </xsl:text>

</xsl:template>

<xsl:template name="rightpad"> 
<xsl:param name="fieldvalue" select="string('')"/>
<xsl:param name="fieldsize" select="0"/>
<xsl:variable name="whatsthediff" select="$fieldsize -
string-length($fieldvalue)" />
<xsl:choose>
<xsl:when test="$whatsthediff < 0" >
<xsl:value-of select="$fieldvalue"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$fieldvalue"/>
<xsl:call-template name="space"><xsl:with-param name="repeat"
select="$whatsthediff"/></xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="leftpad"> 
<xsl:param name="fieldvalue" select="string('')"/>
<xsl:param name="fieldsize" select="0"/>
<xsl:variable name="whatsthediff" select="$fieldsize -
string-length($fieldvalue)" />
<xsl:choose>
<xsl:when test="$whatsthediff < 0" >
<xsl:value-of select="$fieldvalue"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="space"><xsl:with-param name="repeat"
select="$whatsthediff"/></xsl:call-template>
<xsl:value-of select="$fieldvalue"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="space">
<xsl:param name="repeat">0</xsl:param>
<xsl:param name="fillchar" select="' '/>
<xsl:if test="number($repeat) >= 1">
<xsl:call-template name="space">
<xsl:with-param name="repeat" select="$repeat - 1"/>
<xsl:with-param name="fillchar" select="$fillchar"/>
</xsl:call-template>
<xsl:value-of select="$fillchar"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Steve Yates wrote:
> 
> I am need of a way to take an XML file and move that information into a
> fixed width, space delimited text file. How can I do this?
> 
> Thanks!
> 
> Steve
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

--
Mitch.Amiano (@alcatel.com)
SW Development Engineer in C++/Java/Perl/TCL/SQL/XML/XSLT/XPath
Advance Design Process Group, Raleigh Engineering Services     Alcatel
USA

 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.