Re: recursion and looping

Subject: Re: recursion and looping
From: Phil Lanch <phil@xxxxxxxxxxxxxxx>
Date: Thu, 23 Mar 2000 19:29:41 +0000
Francois Lachance wrote:
> 
> AIM - group elements according to attribute  value [indeterminate number
> of values
> Strategy - apply tempates subsequent subsets of nodeset
> Problem - how to build the recursion without infinte  loop
> 
> Question: is the looping induced by the following stylesheet casued by a
> failure to shift context node? If so how can one pass a param value that
> cause a shift in context node? Or is this totally wrong-tree barking?

call-template, unlike apply-templates, doesn't change the context node.  i
think call-template _is_ the one you want, because it calls the grouper
template _once_ (not once for each node), which lets grouper handle the,
er, grouping.  also, a select attribute is only meaningful on
apply-templates, not on call-template-

> <xsl:call-template name="grouper" select="//w[@function='item']">

-, so you need to pass "//w[@function='item']" to grouper as the value of
another param.  btw, if you know where the <l>s will be, it's probably
better to say "/lg/l/w[@function='item']": // is always likely to be
inefficient.

i think you're trying to make $listname change form 'list1' to 'list2' to
'list3' etc on each call of grouper??  if i'm right about that, then i'd
say it's easier to pass the numbers 1, 2, 3, etc & make strings from the
numbers when necessary - because it's easier to increment numbers than
strings (this isn't Perl).  so the lg template could say-

<xsl:call-template name="grouper">
<xsl:with-param name="nodes" select="/lg/l/w[@function='item']"/>
<xsl:with-param name="listnumber" select="1"/>
</xsl:call-template>

-and then grouper could do something like-

<xsl:param name="nodes"/>
<xsl:param name="listnumber"/>

<xsl:param name="listname" select="concat('list',$listnumber)/>
<xsl:param name="nodeset" select="$nodes/self::*[@n=$listname]"/>
<xsl:param name="nextset" select="$nodes/self::*[@n!=$listname]"/>
<xsl:param name="nextlistnumber" select="$listnumber+1"/>

<xsl:if test="$nodes">
<!-- i.e. "are there any nodes that haven't been handled yet?";
whereas "not($nextset='')" is true when there are no nodes in $nextset that
have a string value of '' - which looks like why you're getting infinite
recursion (it's true for the empty node-set) -->

(no change to your for-each loop.)

<xsl:call-template name="grouper">
<xsl:with-param name="nodes" select="$nextset"/>
<xsl:with-param name="listnumber" select="$nextlistnumber"/>
</xsl:call-template>

</xsl:if>

-- 

cheers

phil

"When Alkan said, 'How are you?' the question had a total nuance:
he really wanted to know how you were, although at the same time
he was asking the question purely for the sake of social from.
Yet he managed simultaneously to acknowledge both of these
conflicting messages and still reformulated the question so that
it incorporated them and yet was devoid of all assumptions.
Furthermore none of the above seemed to be _implied_."


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


Current Thread