[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: to extract the longest string (fwd)


Subject: Re: to extract the longest string (fwd)
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 18 Aug 2000 10:08:03 +0100

Rajagopal,

>I want to concatnate the 'align' attributes of the <col> under each 
><row>  into  separate strings and after that I want to extract the
>lengthiest of the strings.

Can I ask a couple of questions first?

In your example, all the col elements within a row have the same value for
the 'align' attribute.  Is this always the case?

Is it just the concatenation of all the align attributes that you're after,
or is it important that the strings are made up of the same character
repeated?  If there were a row with:

<row>
  <col align="l" />
  <col align="c" />
  <col align="c" />
  <col align="r" />
</row>

would you be after the string "lccr" or "cc" for that row?

I'm going to assume for now that the answer is that you are after the
longest string, and that this string will *always* be that generated from
the row with the largest number of columns.  This makes things a lot
easier.  But I'm also going to assume that you want the string for a
purpose other than to uncover how many columns there are within a row - if
you're aim is to do that, you don't need to fiddle with align attributes to
do it.

First a template to manage the processing, and to set it off correctly.
You can put any other processing that you want to do for the table within
this template, and use the string in the way that you want to use it.

<xsl:template match="table">
  <xsl:variable name="longest-align-string">
    <xsl:apply-templates select="." mode="get-longest-align-string" />
  </xsl:variable>
  Longest align string: <xsl:value-of select="$longest-align-string" />
</xsl:template>

The template that actually gets the longest align string is next.  It looks
at all the rows with a xsl:for-each and sorts them according to which has
the highest number of columns by counting the columns and arranging them in
descending order.  An xsl:if is then used to pluck off the first of this
list: if the position() = 1 then it's the first in the list, the row with
the highest number of columns.  Then, for each column within that row, it
outputs the value of the 'align' attribute - these get concatenated
together as they are outputted one after each other.  So the template
returns the string into the variable as above.

<xsl:template match="table" mode="get-longest-align-string">
  <xsl:for-each select="row">
    <xsl:sort select="count(col)" order="descending" />
    <xsl:if test="position() = 1">
      <xsl:for-each select="col">
        <xsl:value-of select="@align" />
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

This solution has been tested and works in Xalan and SAXON.

Jeni

Jeni Tennison
http://www.jenitennison.com/



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



Current Thread