BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


Uncategorized
Re: Swapping values in two integer variables without using a temporary variable
Swapping values in two integer variables without using a temporary variable
Comparing Multiple fields from the subquery
Re: Filtering unique values
Re: Grouping with totals/subtotals -- nested Muenchian method
Grouping with totals/subtotals -- nested Muenchian method
Generic JScript Function To Load XML Data
WebCube Report - Browser-based Multidimensional Data Reporting
The pow(a, b) function
Re: Missing constant definition
A simple template to display singular and plural counts
swapping two integer values without using temporary
Debugging script
Combinations of k out of n numbers
Permutations of the values of N nodes
What is BEEP (BXXP)?
Family Tree maker
Alternative Ordering - ""Women and Children to the lifeboats first""
Meta-Patterns: Design Patterns Explained
XML Helper functions in Visual Basic


 
 

<< System.XMLWCF, WS, SOAP >>


By Dimitre Novatchev
First Posted 03/20/2001
Times viewed 283

Padding with spaces to a fixed-length string


Summary This snippet shows how to pad a given string with spaces to a given fixed length. Two non-recursive templates are presented. Demonstrated is how to specify white-space only nodes within a variable's contents.

This is a repeated FAQ: "I have a string, which I need to pad with spaces so that the result will occupy exactly 10 characters. Can this be done with XSLT?" The answer is an "yes" as demonstrated by the code below. While a general recursive approach for iterating N times and outputting a space on each iteration, here we present another, non-recursive approach. The idea is that if we know in advance the maximum length to which strings will have to be padded, then we could declare an xsl:variable holding a string of spaces with at least this maximum length. The rest is just some simple calculation of lengths and using the substring function. The code below performs right-padding a string. A very small modification is needed so that left-padding will be performed just change: substring(concat($String,$spaces),1,$length) to concat(substring($spaces, 1, $length - string-length($String)), $String) Another thing to note is the xml:space="preserve" in one of the definitions of the "spaces" variable. Why is this necessary? As we know, the msxml3 parser discards on default all whitespace-only nodes. Without specifying the "preserve" value for the xml:space attribute, it would not be possible to specify the necessary number of spaces as a single text node within the contents of the variable. ANother thing to note is that if we intend to present the resulting padded string by a browser, we need to enclose it with
 (or , or 
) tags, or translate all spaces to so that the browser will not ignore the spaces.

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< System.XMLWCF, WS, SOAP >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML