Angela_SB
Angela_SB

Reputation: 249

XSLT Split a text in fixed width lines

I have a text which I want to process using XSLT. Right now all my text is truncated to 40 chars, like this:

<xsl:call-template name="justify">
  <xsl:with-param name="value" select="comment"/>
  <xsl:with-param name="width" select="40"/>
  <xsl:with-param name="align" select=" 'center' "/>
</xsl:call-template>

But now I would like to display the whole text on many lines, each with 40 chars. How can this be achieved? Angela

These are the templates I use:

  <xsl:template name="justify">
    <xsl:param name="value" />
    <xsl:param name="width" select="10"/>
    <xsl:param name="align" select=" 'left' "/>

    <xsl:variable name="output" select="substring($value,1,$width)"/>

    <xsl:choose> 
      <xsl:when test="$align = 'center'">
        <xsl:call-template name="dup">
          <xsl:with-param name="input" select=" ' ' "/>
          <xsl:with-param name="count"
            select="floor(($width - string-length($output)) div 2)"/>
        </xsl:call-template>
        <xsl:value-of select="$output"/>
        <xsl:call-template name="dup">
          <xsl:with-param name="input" select=" ' ' "/>
          <xsl:with-param name="count"
            select="ceiling(($width - string-length($output)) div 2)"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="dup">
    <xsl:param name="input"/>
    <xsl:param name="count" select="1"/>
    <xsl:choose>
      <xsl:when test="not($count) or not($input)"/>
      <xsl:when test="$count = 1">
        <xsl:value-of select="$input"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$count mod 2">
          <xsl:value-of select="$input"/>
        </xsl:if>
        <xsl:call-template name="dup">
          <xsl:with-param name="input"
               select="concat($input,$input)"/>
          <xsl:with-param name="count"
               select="floor($count div 2)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

And the XML is very basic:

<Receipt>
 <store>
    <name>This is my new store</name> 
    <storeID>10000</storeID> 
    <addressline /> 
  </store>

  <transaction>
    <!-- some other nodes hier-->
  </transaction>

  <comment>Here comes a very long comment that needs to be displayed on many lines, 40 chars each.</comment>   
</Receipt>

I have used something like this

    <xsl:template name="for">
      <xsl:param name="value"/>
      <xsl:param name="start">1</xsl:param>
      <xsl:param name="stop"/>
      <xsl:param name="step">40</xsl:param>
      <xsl:text/>
      <xsl:if test="$start &lt; $stop">
         <xsl:call-template name="justify">
            <xsl:with-param name="value" select="substring($value,$start,$step)"/>
            <xsl:with-param name="width" select="40"/>
            <xsl:with-param name="align" select=" 'center' "/>
        </xsl:call-template>  
          <xsl:text>
</xsl:text>         

        <xsl:call-template name="for">  
          <xsl:with-param name="value">
            <xsl:value-of select="$value"/>
          </xsl:with-param> 
          <xsl:with-param name="stop">
            <xsl:value-of select="$stop"/>
          </xsl:with-param>
          <xsl:with-param name="start">
            <xsl:value-of select="$start + $step"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>

With this I split my input in small strings of the specified length and this is what I wanted to achieve.

Are there any simpler solutions?

Upvotes: 1

Views: 2416

Answers (1)

Angela_SB
Angela_SB

Reputation: 249

I have used something like this

    <xsl:template name="for">
      <xsl:param name="value"/>
      <xsl:param name="start">1</xsl:param>
      <xsl:param name="stop"/>
      <xsl:param name="step">40</xsl:param>
      <xsl:text/>
      <xsl:if test="$start &lt; $stop">
         <xsl:call-template name="justify">
            <xsl:with-param name="value" select="substring($value,$start,$step)"/>
            <xsl:with-param name="width" select="40"/>
            <xsl:with-param name="align" select=" 'center' "/>
        </xsl:call-template>  
          <xsl:text>
</xsl:text>         

        <xsl:call-template name="for">  
          <xsl:with-param name="value">
            <xsl:value-of select="$value"/>
          </xsl:with-param> 
          <xsl:with-param name="stop">
            <xsl:value-of select="$stop"/>
          </xsl:with-param>
          <xsl:with-param name="start">
            <xsl:value-of select="$start + $step"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>

With this I split my input in small strings of the specified length and this is what I wanted to achieve.

Upvotes: 1

Related Questions