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


>Something like
><xsl:value-of select="substring(concat($string, '            '), 1, 12))"/>

Just as a challenge, I took it a little further. The following outputs the
color values in this document

  <test>
  <color>red</color>
  <color>blue</color>
  <color>yellow</color>
  </test>

at 12 characters each, right aligned:


  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
  version="1.0">
    <xsl:variable name="fieldWidth">12</xsl:variable>

    <xsl:template match="color">
      <xsl:variable name="valueLength" 
           select="string-length(normalize-space(.))"/>
      <xsl:variable name="padding" 
           select="$fieldWidth - $valueLength"/>

      <xsl:text>[</xsl:text>
      <xsl:value-of select="substring('                  ',1,$padding)"/>
      <xsl:value-of select="."/>
      <xsl:text>]</xsl:text>
    </xsl:template>

  </xsl:stylesheet>


giving this output:


  <?xml version="1.0" encoding="utf-8"?>
  [         red]
  [        blue]
  [      yellow]


I'm starting to like local variables more!

Bob DuCharme          www.snee.com/bob           <bob@  
snee.com>  "The elements be kind to thee, and make thy
spirits all of comfort!" Anthony and Cleopatra, III ii


 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]