Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Adding fixed whitespace to XSLT text output? >Thread Next - Re: Adding fixed whitespace to XSLT text output? RE: Adding fixed whitespace to XSLT text output?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! | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
