[XSL-LIST Mailing List Archive Home]
RE: [xsl] Incrementing a counter
Subject: RE: [xsl] Incrementing a counter|
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 14 Jun 2007 18:37:43 +0100
> I have a problem that I cannot solve and would appreciate some help.
> From what I understand about variables in XSLTs, you can't
> change the value. So if that's true, how can I increment a counter?
You need to think about a higher-level description of your problem, and not
about how you would solve it in an imperative programming language.
> In my scripting experience, I would sort the list by
> category, create a counter and loop through the list of items
> comparing each subsequent item to the previous item. It it
> was the same, I would ignore the category. That may not be
> the most elegant solution, but it works
This "sorting and comparing to the previous category" is a procedural
algorithm for doing grouping. Grouping in XSLT 2.0 is done using
xsl:for-each-group; in 1.0 it's done (clumsily) using keys as described at
Numbering items within a group can be done using the position() function.