VOOZH about

URL: https://www.javacodegeeks.com/2014/10/right-pad-values-with-xslt.html

⇱ right-pad values with XSLT


In this post an XSLT function that can be used to right-pad the value of an element with a chosen character to a certain length. No rocket science but this might become handy again so by putting it down here I don’t have to reinvent it later. The function itself looks like:
 
 
 
 
 
 
 

<xsl:stylesheet version="2.0" xmlns:functx="http://my/functions"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:function name="functx:pad-string-to-length" as="xsd:string">
 <xsl:param name="stringToPad" as="xsd:string?"/>
 <xsl:param name="padChar" as="xsd:string"/>
 <xsl:param name="length" as="xsd:integer"/>
 <xsl:sequence select="
 substring(
 string-join (
 ($stringToPad, for $i in (1 to $length) return $padChar)
 ,'')
 ,1,$length)
"/>
 </xsl:function>

</xsl:stylesheet>

And with this function available you can use it like:

<xsl:template match="/">
 <xsl:value-of select="functx:pad-string-to-length(//short_value, '0', 12)" />
 </xsl:template>

The input XML like:

<test>
 <short_value>123</short_value>
</test>

will give as a result in this case:
123000000000
By the way, this function only works with XSLT2!

Reference: right-pad values with XSLT from our JCG partner Pascal Alma at the The Pragmatic Integrator blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
XML XSLT
πŸ‘ Photo of Pascal Alma
Pascal Alma
October 21st, 2014Last Updated: October 21st, 2014
0 1,047 1 minute read

Pascal Alma

Pascal is a senior JEE Developer and Architect at 4Synergy in The Netherlands. Pascal has been designing and building J2EE applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Specialties: JEE, SOA, Mule ESB, Maven, Cloud Technology, Amazon AWS.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz