XSLT String Padding

I had the need to pad values in a xsl template from a BizTalk map. These helpful call templates did the trick:

http://www.dpawson.co.uk/xsl/sect2/padding.html

<xsl:template name=”prepend-pad”>
<!– recursive template to right justify and prepend–>
<!– the value with whatever padChar is passed in   –>
<xsl:param name=”padChar”/>
<xsl:param name=”padVar”/>
<xsl:param name=”length”/>
<xsl:choose>
<xsl:when test=”string-length($padVar) &lt; $length”>
<xsl:call-template name=”prepend-pad”>
<xsl:with-param name=”padChar” select=”$padChar”/>
<xsl:with-param name=”padVar” select=”concat($padChar,$padVar)”/>
<xsl:with-param name=”length” select=”$length”/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=”substring($padVar,string-length($padVar) –
$length + 1)”/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name=”append-pad”>
<!– recursive template to left justify and append  –>
<!– the value with whatever padChar is passed in   –>
<xsl:param name=”padChar”/>
<xsl:param name=”padVar”/>
<xsl:param name=”length”/>
<xsl:choose>
<xsl:when test=”string-length($padVar) &lt; $length”>
<xsl:call-template name=”append-pad”>
<xsl:with-param name=”padChar” select=”$padChar”/>
<xsl:with-param name=”padVar” select=”concat($padVar,$padChar)”/>
<xsl:with-param name=”length” select=”$length”/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=”substring($padVar,1,$length)”/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Advertisements