Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: Adding fixed whitespace to XSLT text output?

From: MukulGandhi@-----------.---------.---
To: NULL
Date: 3/9/2005 10:19:00 PM
You can write a recursive named templated to do this.. Below is the complete 
XSLT..

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
 
 <xsl:output method="text" /> 

 <xsl:template match="/root">
   <xsl:variable name="new-str">
     <xsl:call-template name="str-pad">
       <xsl:with-param name="string" select="A" />
       <xsl:with-param name="pad-char" select="' '" />
       <xsl:with-param name="str-length" select="20" />
     </xsl:call-template>
   </xsl:variable>
   
   New string = <xsl:value-of select="$new-str" /> , Length = <xsl:value-of 
select="string-length($new-str)" />
   
 </xsl:template>
 
 <xsl:template name="str-pad">
   <xsl:param name="string" />
   <xsl:param name="pad-char" />
   <xsl:param name="str-length" />
   
   <xsl:choose>
     <xsl:when test="string-length($string) > $str-length">
       <xsl:value-of select="substring($string,1,20)" />   
     </xsl:when>
     <xsl:when test="string-length($string) = $str-length">
       <xsl:value-of select="$string" />   
     </xsl:when>
     <xsl:otherwise>
       <xsl:call-template name="str-pad">
         <xsl:with-param name="string" select="concat($pad-char,$string)" />
         <xsl:with-param name="pad-char" select="$pad-char" />
         <xsl:with-param name="str-length" select="$str-length" />
       </xsl:call-template>
     </xsl:otherwise>     
   </xsl:choose>   
 </xsl:template>
 
</xsl:stylesheet>

For e.g. when it is applied to this XML -
<?xml version="1.0"?>
<root>
  <A>hello</A>  
</root>

it produces output -
New string =                hello , Length = 20

Here I am doing left padding. If you wish, you may do the right padding 
similarly.. For this you need to replace <xsl:with-param name="string" 
select="concat($pad-char,$string)" /> with <xsl:with-param name="string" 
select="concat($string, $pad-char)" /> ...

Regards,
Mukul

"jhouk" wrote:

> Hi all,
> 
> I'm a newbie to the XPATH/XSLT world, but I think there could be some
> real value in scaling this learning curve in order to allow my group to
> set up some source data that could be transformed in a variety of
> different ways without having to write a new program every time we want
> to build a new translation for the data.
> 
> Unfortunately, as a newbie I'm having some trouble with my first
> transform, and digging around the books and web pages on XPATH/XSLT
> I've found so far haven't helped much I'm afraid.
> 
> Here's the issue:  I want to take an element from an XML form and turn
> the text of the element into a fixed-length string of, say, 20
> characters.
> 
> I know I can use the xsl:value-of element to get the text of the XML
> element I want, but I haven't had any luck finding out how to use XPATH
> to create the proper string expression to pad the results of the
> value-of operation with whitespace up to 20 characters.  Can anyone
> help?
> 
> Thanks in advance!



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent