This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Fixed-length String Output


Deirdre -
Here are the string padding functions that I use frequently:

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


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

These will both ensure that the string ends up to be whatever length you
pass in (ie. if the string is longer than expected it will also truncate).

I hope this helps.

-Nate

The opinions expressed are my own and do not reflect those of Decision
Systems, Inc.

>Date: Wed, 4 Oct 2000 16:27:36 +0100
>From: "Deirdre O'Brien" <ddo@qad.com>
>Subject: Fixed-length String Output
>
>Hi,
>
>I am trying to transform an incoming XML message to a format that will be
>acceptable to our existing software package.  However the existing package
>excepts data on a fixed-length basis.  This is the way we wish to continue.
>
>I was wondering is there a way to force the output of select statement to
>take up a certain amount of space.  Not all of the elements will be
>numbers.  What I need is something like the format-number function but for
>strings.  I taught maybe there might be an attribute in value-of that would
>do it, but there doesn't appear to be. Is there anything currently
>available in XSLT that would do this for me? I am using the Xalan
>processor.
>
>Thanks for the help I have received so far.  I have ordered a copy of "XSLT
>Programmer's Reference".  This hopefully will enable me to answer my own
>questions.
>
>Thanks again,
>Deirdre O'Brien


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]