xsl-list
[Top] [All Lists]

Re: [xsl] recursion

2019-01-24 02:39:10
I don't know if it's the cause of your problem, but you've fallen into a 
classic elephant trap here by writing 

<xsl:with-param name="number"><xsl:value-of select="$number + 
1"/></xsl:with-param>

instead of

<xsl:with-param name="number" select="$number + 1"/>

Usually this is only a problem of code efficiency and readability, but in this 
case it actually affects the logic. The way you have written it, $number is a 
document node (or in 1.0, a result tree fragment), so $notes[$number] is always 
true, because the effective boolean value of a node is always true. You 
obviously intended this to be a numeric selection, which will only be the case 
if $number is indeed a number.

General rule: declare the types of your variables and parameters, and many of 
your bugs will be found in a fraction of the time.

Michael Kay
Saxonica

On 24 Jan 2019, at 03:34, Jim Albright jim_albright(_at_)wycliffe(_dot_)org 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Problem:  I have an XML file that has notes mixed up with comments.  I want 
to move the notes from current location to Notes element.  There could be 0 
to 10 notes.
Solution:  I will first copy notes found in   
span[@class='x']    
from current location to Notes.  With a second XSLT I will delete the 
unwanted ones.

       <xsl:variable name="notes" 
select="ancestor::Lexicon_Entry//LEXMeanings[1]//LEXSense[@LanguageCode='pt']//span[@class='x']"/>

Recursive code XSLT 2.0:
   <xsl:template name="get_note">
       <xsl:param name="notes" select="."/>
       <xsl:param name="number" ><xsl:value-of select="."/></xsl:param>
       number /<xsl:value-of select="$number"/>/       
       notes[number] /<xsl:value-of select="$notes[$number]"/>/       
       notes /<xsl:value-of select="$notes"/>/       
       <xsl:choose>
           <xsl:when test="exists($notes[$number])">
               <xsl:element name="note">    
               <xsl:attribute name="Caller"><xsl:value-of 
select="$number"/></xsl:attribute>
               <xsl:attribute name="LanguageCode">pt</xsl:attribute>
               <xsl:element name="Content">
                   <xsl:copy-of select="$notes[$number]"/>
               </xsl:element>
           </xsl:element>
               <xsl:call-template name="get_note">
                   <xsl:with-param name="notes" select="$notes"/>
                   <xsl:with-param name="number"><xsl:value-of 
select="$number + 1"/></xsl:with-param>
               </xsl:call-template>
           </xsl:when>
           <xsl:otherwise>
           zzzzzzzz    <!-- end -->    
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>

This works fine for 0 or 1 notes but when 2 or more are found it goes into an 
endless loop.
Here is output with debugging code showing values
   <Note Caller="1" LanguageCode="en">
       <References/>
       <Content>There are no doubt certain subtle differences of meaning in 
these terms, especially in their connotations, but it is not possible to 
specify the difference on the basis of existing contexts.</Content>
     </Note>
   <Note Caller="1" LanguageCode="pt">
             <Content>
             <span class="x">2 Com certeza existem sutis diferenças de 
significado entre esses termos, especialmente em termos conotativos, mas não 
é possível determinar isso com base nos contextos disponíveis. 
</span></Content></Note>


<note Caller="2" LanguageCode="pt"><Content><span class="x">3 Em Mt 5.21, 
<span lang="gk">κρίσις</span> se refere, provavelmente, a um tribunal local. 
</span></Content></note>


       number /3/       
       notes[number] /2 Com certeza existem sutis diferenças de significado 
entre esses termos, especialmente em termos conotativos, mas não é possível 
determinar isso com base nos contextos disponíveis.  3 Em Mt 5.21, κρίσις se 
refere, provavelmente, a um tribunal local. /       
       notes /2 Com certeza existem sutis diferenças de significado entre 
esses termos, especialmente em termos conotativos, mas não é possível 
determinar isso com base nos contextos disponíveis.  3 Em Mt 5.21, κρίσις se 
refere, provavelmente, a um tribunal local. /       

so when $number is 3, $notes[$number] is the same as $notes but $notes[3] is 
the empty set.  
I have verified that $number is actually an integer.  In this example there 
are two notes. The first note starts with 2 Com ... and the second note 
starts with 3 Em ...

This puts me in an endless loop as it always finds something in 
$notes[$number]


Jim Albright
704-562-1529 unlimited cell
Wycliffe Bible Translators



--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>