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

Re: [xsl] Incrementing a Global variable


Subject: Re: [xsl] Incrementing a Global variable
From: Bill Keese <billk@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 29 Aug 2003 09:23:15 +0900

As others said, for-each-group is only available in Saxon v7. Distinct-values support should be available in other engines (see http://www.exslt.org/set/index.html), but I don't know what engine you are using. (I saw your list of programs below but which one is the XSLT processor?)

Why did I use these V2 functions? I was indirectly replying to Mukul's request to make your kind of problem easier to solve in XSLT V2.

Clearly there are many ways to solve this problem. (recursion, two-pass approach, row-number-formula) I was merely trying to directly answer the original question, which I read as "how do you do variables in XSLT?", and which I answer as "use an expression (formula) instead of a variable". Or use recursion, or a two pass algorithm...

Bill

PS: my post had an error in it, I think. To figure out the current group number (given the current record), you should look at the distinct TOption values from all the previous records + the current record. You should say something like count(distinct-nodes(.|preceding-siblings::*/TOption))


Rajendra S Rawat wrote:


Hi! Bill

Your solution seems more logical.so I stopped.And
first gave it a try. But I could not use it, firstly
distinct-values() function raised an function not
found exception. Same happened with
<xsl:for-each-group. I know its an XSLT2.0 answer to
Muenchian Method.

I'am using Win2000 Professional on PIV machine
-XML SPY Professional v5 Rel2 -Apache FOP 0.20.5 -J2Sdk 1.4.2 with netbeans
-IE6 sp1
-Acrobat Reader 5.05



I changed in xslt file <xsl:stylesheet version="2.0" but same error bombed

What else should I do to use XSLT2.0. Do I need to
install some thing?

should i post the actual code?

Regards,
Raj

--- Bill Keese <billk@xxxxxxxxxxxxxxxxxxxx> wrote:


(this is a resend; my original message bounced, I
think)

Problem: print a list of students, grouping the
students by their language and printing a blank line between each
group. Number the blank lines in addition to the lines with students' names.


So calling position() to get the number is
insufficient, because position() doesn't count the blank lines.


Mukul was commenting that this problem would be
easier to solve if XSLT supported something like variables. For example:


 <xsl:for-each select="Student">
      <xsl:if test="TOption !=
previous-sibling::Student[last()]/TOption">
           <tr> <td> {counter} </td>  <td/> <td/>
</tr>
           counter++;
      </xsl:if>

<tr> <td> {counter} </td> <td> {Name} </td>
<td> { Toption } </td> </tr>
counter++;
</xsl:for-each>


This is what Jarno's code essentially does, but
Jarno had to use recursion to simulate the loop, which is [arguably]
a bit cumbersome. However, you can do something the loop above, if you
replace the variable "counter" with a formula, which is
the previous number of students + the previous
number of groups


In XSLT version 2 this is simply:

count(preceding-sibling::*) +



count(distinct-values(preceding-sibling::*/TOption/text()))


And after the loop finishes, to compute the total
number of lines printed, you would do


count(Student) +
count(distinct-values(Student/TOption/text()))

-------------
By the way, an alternative to a single loop is to
write a nested loop (for-each language / for-each student). This is
what Americo's code does, but his code is a bit complicated because he
is programming in XSLT V1, where there is no for-each-group operator (http://www.w3.org/TR/xslt20/#d0e15262) . But in
XSLT V2 we would say this:


<xsl:for-each-group select="Student"
group-by="TOption">
<xsl:variable name="group-no"
select="position()"/>
<xsl:for-each select="current-group()">
<tr>
<td> <xsl:value-of
select="count(preceding-sibling::*) + $group-no"/> </td>
<td> <xsl:value-of select="Name"/> </td>
<td> <xsl:value-of select="TOption"/>
</td>
</tr>
</xsl:for-each>
<tr>
<td> <xsl:value-of




select="count(current-group()[last()]/preceding-sibling::*)


+ $group-no + 1"/> </td>
<td> </td>
<td> </td>
</tr>
</xsl:for-each-group>


Bill


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






__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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







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



Current Thread
Keywords