Tuesday, February 25, 2020

Trimming and Padding characters in XSLT

We had this specific requirement from a service provider to ensure that the value sent for a field is always of fixed length. If value from any front end application is less than mentioned length, we, in BPEL, have to append or prepend the value with some character until the length is reached.
This is also called padding characters for a field.

Note: This blog does not explain how to create an XSLT and basics of XSLT.

I would take an example and explain how it is achieved.

Prepend characters or left padding -  Field length needed is 5 characters. If the value of the field is less than 5 characters, prepend character 'a' to the start of the value.

I have used the following code snippet - 


<xsl:template name="prepend-pad">
<!—recursive template to right justify and prepend the value with whatever padChar is passed-->
 <xsl:param name="padChar"/>
 <xsl:param name="padVar"/>
 <xsl:param name="length"/>
 <xsl:choose>
  <xsl:when test="string-length($padVar) < $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>
 

Above template is kind of reusable function which is be called from multiple places in the XSLT.
padChar - Is the character with which you would want the padding to happen.
padVar - The actual value which needs padding.
length - The length to which you need to adhere.

All I need to do is call the above template from my XSLT for whatever field, padding needs to be applied.

Call template is done as follows -
<xsl:call-template name="prepend-pad">
 <xsl:with-param name="padChar" select = "a" />
 <xsl:with-param name="padVar" select = "blog" />
 <xsl:with-param name="length" select = "5" />
</xsl:call-template>

This would give me the following result

Say -
padVarpadCharlengthresult
bloga5ablog
ga5aaaag


Append characters or right padding - Field length needed is 5 characters. If the value of the field is less than 5 characters, append character 'a' to the end of the value.


<xsl:template name="append-pad">
<!—recursive template to left justify and append the value with whatever padChar is passed-->
 <xsl:param name="padChar"/>
 <xsl:param name="padVar"/>
 <xsl:param name="length"/>
 <xsl:choose>
  <xsl:when test="string-length($padVar) < $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>
 

Call the above template from XSLT similar to the first example.

This would give me the following result

Say -
padVarpadCharlengthresult
bloga5bloga
ga5gaaaa

In this way, we can ensure that the field values are padded according to the requirement.


1 comment:

  1. Thank you for the detailed explanation. it helped me in understanding one of my xslt transformation.

    ReplyDelete