This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: to extract the longest string. (fwd)


>Date: Mon, 21 Aug 2000 23:56:07 +0100
>To: "C.V. Radhakrishnan" <cvr@md2.vsnl.net.in>
>From: Jeni Tennison <mail@jenitennison.com>
>Subject: Re: to extract the longest string. (fwd)
>
>Rajagopal,
>
>>Would you please explain to me how calling this template on the second row
>>element returns the longest result from all  "the rest of rows" and how
>>the recursion is working in that place?
>
>OK.  Let's use an example like:
>
><rows>
>  <row>
>   <col align="abc" />
>   <col align="ab" />
>  </row>
>  <row>
>   <col align="abcd" />
>   <col align="abc" />
>  </row>
>  <row>
>    <col align="ab" />
>    <col align="a" />
>  </row>
></rows>
>
>The template is:
>
><xsl:template match="row" mode="get-longest">
>  <xsl:variable name="current">
>    <xsl:for-each select="col">
>      <xsl:value-of select="@align" />
>    </xsl:for-each>
>  </xsl:variable>
>  <xsl:variable name="longest">
>    <xsl:apply-templates select="following-sibling::row[1]"
>                         mode="get-longest" />
>  </xsl:variable>
>  <xsl:choose>
>    <xsl:when test="string-length($longest) > string-length($current)">
>      <xsl:value-of select="$longest" />
>    </xsl:when>
>    <xsl:otherwise><xsl:value-of select="$current" /></xsl:otherwise>
>  </xsl:choose>
></xsl:template>
>
>When the template is called on the first row, the $current variable is set
to the concatenation of all the 'col' child elements' @align attributes,
i.e. "abcab".
>
>The $longest variable is set to the result of applying templates to the
next row in the same mode, which is the second row...
>
>When the template is called on the second row, the $current variable is
set to the concatenation of all the 'col' child elements' @align
attributes, i.e. "abcdabc".
>
>The $longest variable is set to the result of applying templates to the
next row in the same mode, which is the third row...
>
>When the template is called on the third row, the $current variable is set
to the concatenation of all the 'col' child elements' @align attribute,
i.e. "aba".
>
>The $longest variable is set to the result of applying templates to the
next row in the same mode.  Since there are no remaining rows, the result
is the empty string, "".
>
>Then (still looking at the template applied to the third row), we enter
the choose statement - is the string length of $longest greater than the
string length of $current?  $longest is "" and $current is "aba", so the
answer is no - in that case, the value of $current is returned, i.e. "aba".
 So this value forms the value for $longest for the application of the
template on the second row...
>
>So, looking at the template applied to the second row, the choose
statement - is the string length of $longest greater than the string length
of $current?  $longest is "aba" (got that from the application of the
template on the third row) and $current is "abcdabc", so the answer is no -
in that case, the value of $current is returned, i.e. "abcdabc".  So this
value forms the value for $longest for the application of the template on
the first row...
>
>So, looking at the template applied to the second row, the choose
statement - is the string length of $longest greater than the string length
of $current?  $longest is "abcdabc" (got that from the application of the
template on the third row) and $current is "abcab", so the answer is yes -
in that case, the value of $longest is returned, i.e. "abcdabc".  So this
value is the result of the application of the template on the first row.
>
>So the final answer is "abcdabc".
>
>>Normally what I do for finding out the longest string is that 
>>I will store the first string in a temp variable and subsequently 
>>check all the following strings with this temp to find out the longest.
>>
>>A process similar to this is not taking place here. Why? 
>
>It's really because XSLT is a declarative language rather than a
procedural one.  You can't set variables and then look at their values
unless you use the node-set() extension function, so at any point you only
know about what you've been told, and you have to use that to work out the
answer.  This means that recursive solutions like this one are a lot more
effective.
>
>I hope this clears things up a bit,
>
>Jeni
>
Jeni Tennison
http://www.jenitennison.com/


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]