Recursive xsl call template, or padding in xsl 1.0

 Uncategorized  Comments Off on Recursive xsl call template, or padding in xsl 1.0
Nov 022012
 

So I needed to fill a value up to 24 characters. I decided to do a bit of research so I could hold my own to my brother Brian (who currently holds the title at Stott Creations as the XSL king).

I have never used xsl:call-template nor recursion.

Here is an example of how to do this:

<?xml-stylesheet type="text/xsl" href="padding.xsl"?> <!--The most important is the <xsl:call-template>--> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output indent="yes"/> <xsl:template match="/"> <XML> <Example> <xsl:attribute name="Instance"> <xsl:text>1</xsl:text> </xsl:attribute> <Title>Padding zeros at the end of A435 into a 6 byte field</Title> <Value> <xsl:call-template name="startpadding"> <xsl:with-param name="padChar" select="'0'"/> <xsl:with-param name="padCount" select="6"/> <xsl:with-param name="actualvalue" select="'A435'" /> <xsl:with-param name="justification" select="'left'" /> </xsl:call-template> </Value> </Example> <Example> <xsl:attribute name="Instance"> <xsl:text>2</xsl:text> </xsl:attribute> <xsl:variable name="pad_tmp_1"> <xsl:call-template name="startpadding"> <xsl:with-param name="padCount" select="40"/> <xsl:with-param name ="padChar" select="'#'" /> <xsl:with-param name="actualvalue" select="'Look ma, spaces:'" /> <xsl:with-param name="justification" select="'left'" /> </xsl:call-template> </xsl:variable> <xsl:variable name="pad1" select="translate($pad_tmp_1,'#',' ')"/> <Title>Dealing with spaces with a 40 character field (left justified)</Title> <Value> <xsl:value-of select="$pad1"/> </Value> </Example> <Example> <xsl:attribute name="Instance"> <xsl:text>3</xsl:text> </xsl:attribute> <xsl:variable name="pad_tmp_2"> <xsl:call-template name="startpadding"> <xsl:with-param name="padCount" select="40"/> <xsl:with-param name ="padChar" select="'#'" /> <xsl:with-param name="actualvalue" select="'...and then there is text'" /> <xsl:with-param name="justification" select="'right'" /> </xsl:call-template> </xsl:variable> <xsl:variable name="pad2" select="translate($pad_tmp_2,'#',' ')"/> <Title>Dealing with spaces with a 40 character field (right justified)</Title> <Value> <xsl:value-of select="$pad2"/> </Value> </Example> </XML> </xsl:template> <xsl:template name="startpadding"> <xsl:param name="padChar" select="'#'"/> <xsl:param name="padCount" select="0"/> <xsl:param name="actualvalue" select="0"/> <xsl:param name="justification" select="left" /> <xsl:choose> <xsl:when test="$justification='left'"> <xsl:value-of select="$actualvalue"/> <xsl:variable name="valuelength"> <xsl:value-of select="string-length($actualvalue)"/> </xsl:variable> <xsl:if test="$valuelength &lt; $padCount"> <xsl:call-template name="pad"> <xsl:with-param name="padChar" select="$padChar" /> <xsl:with-param name="padCount" select="$padCount - $valuelength" /> </xsl:call-template> </xsl:if> </xsl:when> <xsl:otherwise> <xsl:variable name="valuelength"> <xsl:value-of select="string-length($actualvalue)"/> </xsl:variable> <xsl:if test="$valuelength &lt; $padCount"> <xsl:call-template name="pad"> <xsl:with-param name="padChar" select="$padChar" /> <xsl:with-param name="padCount" select="$padCount - $valuelength" /> </xsl:call-template> </xsl:if> <xsl:value-of select="$actualvalue"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="pad"> <xsl:param name="padChar" select="'#'"/> <xsl:param name="padCount" select="0"/> <xsl:value-of select="$padChar"/> <xsl:if test="$padCount&gt;1"> <xsl:call-template name="pad"> <xsl:with-param name="padCount" select="number($padCount) - 1"/> <xsl:with-param name="padChar" select="$padChar"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>

Which returns this:

<?xml version="1.0" encoding="utf-8"?> <XML> <Example Instance="1"> <Title>Padding zeros at the end of A435 into a 6 byte field</Title> <Value>A43500</Value> </Example> <Example Instance="2"> <Title>Dealing with spaces with a 40 character field (left justified)</Title> <Value>Look ma, spaces: </Value> </Example> <Example Instance="3"> <Title>Dealing with spaces with a 40 character field (right justified)</Title> <Value> ...and then there is text</Value> </Example> </XML>