Re: [xsl] Print number of chars depending upon int value

Subject: Re: [xsl] Print number of chars depending upon int value
From: Elliotte Harold <elharo@xxxxxxxxxxxxxxx>
Date: Fri, 22 Apr 2005 07:11:08 -0400
Ranjan K. Baisak wrote:
select="substring('...................................',
1, $myLength)"/> should do the trick!

But if $myLength is greater than the number of chars in the string?


Like most things you'd accomplish with loops in traditional languages, this can be done in XSLT with recursion and named templates. Here's a simple stylesheet that prints 100 dots.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output indent="yes"/>


  <xsl:template match="/">
    <xsl:call-template name="dots">
      <xsl:with-param name="count">100</xsl:with-param>
    </xsl:call-template>
 </xsl:template>

<xsl:template name="dots">

<xsl:param name="count" select="1"/>

        <xsl:if test="$count &gt; 0">
          <xsl:text>.</xsl:text>
          <xsl:call-template name="dots">
            <xsl:with-param name="count" select="$count -1"/>
          </xsl:call-template>
        </xsl:if>

</xsl:template>


</xsl:stylesheet>



Obviously if you could call the dots template with the count param set to a value provided by the input document rather than a constant as seen here.


--
Elliotte Rusty Harold  elharo@xxxxxxxxxxxxxxx
XML in a Nutshell 3rd Edition Just Published!
http://www.cafeconleche.org/books/xian3/
http://www.amazon.com/exec/obidos/ISBN=0596007647/cafeaulaitA/ref=nosim

Current Thread