Subject: Re: [xsl] Fixed width text output From: "Mitch C. Amiano" <Mitch.Amiano@xxxxxxxxxxx> Date: Wed, 03 Oct 2001 13:22:48 -0400 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Fixed width text output, cutlass | Thread | [xsl] Re: Finding the maximum numbe, Dimitre Novatchev |
Re: [xsl] Disable Output escaping , Thomas B. Passin | Date | [xsl] How to divide footnotes from , Sandeep Deshpande |
Month |