RE: [xsl] Fixed Line Length Output

Subject: RE: [xsl] Fixed Line Length Output
From: Jarno.Elovirta@xxxxxxxxx
Date: Tue, 9 Dec 2003 09:52:51 +0200
Hi,

> Given the following xml fragment
> 
> <item>Len_5</item>
> <item>Len_5</item>
> <item>Length_8</item>
> <item>Length__9</item>
> <item>Length__10</item>
> <item>Len__6</item>
> <item>L_3</item>
> <item>Le_4</item>
> <item>L_3</item>
> ..
> 
> I want to create the following output
> 
> <line>Len_5 Len_5 Length_8</line>
> <line>Length__9 Length__10</line>
> <line>Len__6 L_3 Le_4 L_3 </line>
> 
> I other words , I want to regroup my items into fixed length of lines.

Dimitre already gave you a solution that used a LINE FEED to break the lines, but if you need the line elements, e.g. 

  <xsl:param name="max" select="20"/>
  <xsl:param name="delim" select="' '"/>
  <xsl:param name="pad" select="' '"/>
  <xsl:template match="*[item]">
    <xsl:apply-templates select="item[1]"/>
  </xsl:template>
  <xsl:template match="item">
    <xsl:variable name="count">
      <xsl:call-template name="coil"/>
    </xsl:variable>
    <line>
      <xsl:variable name="line">
        <xsl:for-each select=". | following-sibling::item[position() &lt;= $count]">
          <xsl:if test="not(position() = 1)">
            <xsl:value-of select="$delim"/>
          </xsl:if>
          <xsl:value-of select="."/>
        </xsl:for-each>
      </xsl:variable>
      <xsl:value-of select="$line"/>
      <xsl:call-template name="fill">
        <xsl:with-param name="length" select="string-length($line)"/>
      </xsl:call-template>
    </line>
    <xsl:apply-templates select="following-sibling::item[position() = $count + 1]"/>
  </xsl:template>
  <xsl:template name="coil">
    <xsl:param name="n" select="."/>
    <xsl:param name="l" select="0"/>
    <xsl:param name="i" select="0"/>
    <xsl:choose>
      <xsl:when test="$l + boolean($i)+ string-length($n) &lt;= $max and $n">
        <xsl:call-template name="coil">
          <xsl:with-param name="n" select="$n/following-sibling::item[1]"/>
          <xsl:with-param name="l" select="$l + boolean($i) + string-length($n)"/>
          <xsl:with-param name="i" select="$i + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$i - 1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="fill">
    <xsl:param name="length" select="0"/>
    <xsl:if test="$length &lt; $max">
      <xsl:value-of select="$pad"/>
      <xsl:call-template name="fill">
        <xsl:with-param name="length" select="$length + 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

will get you there. Though, you can tokenize the output from Dimitre's stylesheet to get the line element and add padding. Anyhow.

Cheers,

Jarno - Seabound: Contact

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


Current Thread