[xsl] Re: Fixed Line Length Output

Subject: [xsl] Re: Fixed Line Length Output
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 8 Dec 2003 22:26:32 +0100
"Ali Afif Mutlu" <ali.mutlu@xxxxxxxxxxxx> wrote in message
news:005001c3bdb1$f6eb4260$8928020a@xxxxxxxxxxxxx
> Hi,
>
> >> can you please explain the sentence --
> >> *I want to regroup my items into fixed length of lines*
>
> Assume that the following list is the veteran tenis players of our club
>
> <item>Andre Agassi</item>
> <item>Boris Becker</item>
> <item>Pat Cash</item>
> <item>John McEnroe</item>
> <item>Jimmy Connors</item>
> <item>Stephan Edberg</item>
> <item>Ivan Lendle</item>
>
> I want to display these names on a board where I can fit only 25
> characters on a roe, so on my board
> The list should be as follows (I am not allowed to split first names and
> last names)
>
> Andre Agassi,Boris Becker
> Pat Cash,John McEnroe,Jimmy Connors
> Stephan Edberg,Ivan Lendle

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:call-template name="combineToLength">
      <xsl:with-param name="pmaxLength" select="35"/>
      <xsl:with-param name="pList" select="*/item"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="combineToLength">
    <xsl:param name="pmaxLength"/>
    <xsl:param name="pCurrString"/>
    <xsl:param name="pList" select="/.."/>

    <xsl:choose>
    <xsl:when test="$pList">
      <xsl:variable name="vMyString"
        select="concat($pCurrString,
                       $pList[1][not($pCurrString)]
                       )"/>
        <xsl:variable name="vMyList"
         select="$pList[$pCurrString]
                |
                 $pList[position() > 1][not($pCurrString)]"/>

      <xsl:choose>
          <xsl:when test="string-length($vMyString)
                          +1+string-length($vMyList[1])
                        &lt;=
                          $pmaxLength">
             <xsl:call-template name="combineToLength">
               <xsl:with-param name="pmaxLength" select="$pmaxLength"/>
               <xsl:with-param name="pCurrString"
                select="concat($vMyString, ',', $vMyList[1])"/>
                <xsl:with-param name="pList" select="$vMyList[position() >
1]"/>
             </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat($vMyString, '&#xA;')"/>
            <xsl:call-template name="combineToLength">
               <xsl:with-param name="pmaxLength" select="$pmaxLength"/>
               <xsl:with-param name="pCurrString" select="''"/>
                <xsl:with-param name="pList" select="$vMyList"/>
             </xsl:call-template>

          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$pCurrString"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<players>
  <item>Andre Agassi</item>
  <item>Boris Becker</item>
  <item>Pat Cash</item>
  <item>John McEnroe</item>
  <item>Jimmy Connors</item>
  <item>Stephan Edberg</item>
  <item>Ivan Lendle</item>
</players>

produces the wanted result:

Andre Agassi,Boris Becker,Pat Cash
John McEnroe,Jimmy Connors
Stephan Edberg,Ivan Lendle


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




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


Current Thread