[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Fixed Line Length Output

Subject: Re: Fixed Line Length Output
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 8 Dec 2003 22:26:32 +0100
ivan lendle
"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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.