[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Fixed width text 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|