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

RE: [xsl] Please help Problem for : increment a variable in a for-each?


Subject: RE: [xsl] Please help Problem for : increment a variable in a for-each?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 30 Nov 2004 21:27:45 -0000

Looking back at the thread, it seems that no-one has given you the most
basic information - probably because we see it said so often on this list
that we forget there are people who haven't heard it: YOU CANNOT UPDATE
VARIABLES IN XSLT.

Everyone who has responded to you has hinted at this, by showing you
solutions that don't involve updating variables, but I don't think anyone
said it directly.

In your match="Lists" template you have a variable called $index whose value
is 0. This template calls the named template with name="formatTitle",
specifying

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

The $index in the name attribute refers to a local parameter in the
formatTitle template, while the $index in the select attribute refers to the
variable in the calling match="Lists" template. They are unrelated
variables, so let's change their names to make it clearer:

<xsl:with-param name="$index-B" select="$index-A + 1"/>

$index-A is always 0. Therefore $index-B is always 1.

I hope this explains to you why your code isn't working. It doesn't solve
your problem - but as regulars on this list will know, I often try to help
people understand their problems but I very rarely write substantial chunks
of code for them.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: que Li [mailto:queincanada@xxxxxxxx] 
> Sent: 30 November 2004 18:29
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Please help Problem for : increment a variable 
> in a for-each?
> 
> Hi all:
>   I got so many peopel help but I can't get solution
> for my problem yet. I need fix it asap so please help
> me ! question is : How I can find relatiove position 
> on the node which satify the condition ? I try to set
> the variable called index and increment index value
> when I call template but the index value is always 1
> 
> I work on this problems for several days, I wish I can
> get help for solution.
> 
> Thanks
> 
> 
> 
> xml file:
> <Lists>
> <List >
>   <List_ID>10</List_ID>
>    <Title>A</Title>
>   <Parent_ID>1</Parent_ID>
>  </List>
>  <List>
>    <List_ID>11</List_ID>
>    <Title>A1</Title>
>    <Parent_ID>10</Parent_ID>
>  </List>
> <List >
>     <List_ID>12</List_ID>
>    <Title>B</Title>
>   <Parent_ID>20</Parent_ID>
>  </List>
>  <List >
>    <List_ID>13</List_ID>
>    <Title>A2</Title>
>    <Parent_ID>10</Parent_ID>
>  </List>
>  <List >
>    <List_ID>14</List_ID>
>    <Title>C</Title>
>     <Parent_ID>1</Parent_ID>
>  </List>
>   </Lists>
> 
> 
> xsl:
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
> 
> <xsl:template match="Lists"  >
>         <xsl:apply-templates>
> <xsl:with-param name="ValidCount"
> select="number(count(List)-count(List[Parent_ID=../List/List_ID]))"/>
>  
> </xsl:apply-templates> 	   
> </xsl:template>		
> 
> <xsl:template match="List">
>    <xsl:param name="ValidCount"/>
>    <xsl:variable name="self" select="."/> 
>    <xsl:variable name="index" select="0"/> 
>    <xsl:choose>
>    <xsl:when test="Parent_ID=1 or
> not(preceding-sibling::List[List_ID = $self/Parent_ID]
>  or following-sibling::List[List_ID =
> $self/Parent_ID])"> 
>  <xsl:call-template name="formatTitle">
>   <xsl:with-param name="index" select="$index+1"/>
>   <xsl:with-param name="TotalCount"
> select="$ValidCount"/> 
> <xsl:with-param name="Title" select="Title"/>         
>             
> </xsl:call-template>
> <xsl:call-template name="findChildNode">
>   <xsl:with-param name="ParentID" select="List_ID"/>
> </xsl:call-template> 
> </xsl:when> 
> </xsl:choose>                   
> </xsl:template> 
> 
> <xsl:template name="findChildNode">
> ....this part work
> </xsl:template>
> 
> <xsl:template name="formatTitle">
>  	   <xsl:param  name="index" />
>                   <xsl:param  name="TotalCount"/> 
>                index:  <xsl:value-of select="$index"/>
>                   <xsl:param name="Title"/>	
>                   <xsl:choose>
> <xsl:when test="$TotalCount=1 or $index=1">
>  <b><xsl:value-of select="$Title"/></b>			
> 			
> 						</xsl:when>
> 						<xsl:when 
> test="$index = $TotalCount -1">
> 						     <b> and 
> <xsl:value-of select="$Title"/></b>
> 					
> 						</xsl:when>
> 						<xsl:when 
> test="$index  = $TotalCount">
> 						     <b> 
> <xsl:value-of select="$Title"/></b>				
> 	
> 						</xsl:when>
> 						<xsl:otherwise>
> 						   <b>; 
> <xsl:value-of select="$Title"/></b>	
> 						</xsl:otherwise> 
> 				  </xsl:choose> 
> 	</xsl:template>
> </xsl:stylesheet>
> 
> 
> ______________________________________________________________
> ________ 
> Post your free ad now! http://personals.yahoo.ca


Current Thread
Keywords
xml