Re: xsl:counter

Subject: Re: xsl:counter
From: Mike Brown <mike@xxxxxxxx>
Date: Tue, 20 Jun 2000 09:20:11 -0600 (MDT)
> Is there some way of using a counter in XSL?  I have a list of items, and
> I'd like some extra text to be printed after every 50th item.

For this you don't need to increment a counter; you just need to
look at the position() of the current node. If when divided by 50 the
remainder is zero, add the extra text, like this:

<xsl:template match="item">
  <xsl:text>item text: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:if test="position() mod 50 = 0">
    <xsl:text>some extra text</xsl:text>

> The only things which I've found which look to do this are xsl:counter and
> xsl:counter-reset, but these always seem to be unrecognised by whatever
> XML/XSL parser I use.

There is no counter in XSLT. "xsl:counter" is something Microsoft made up
for MSXML1/IE5. 

You also can't implement a counter in an obvious way, since variables
cannot be updated once they are assigned. A named template can be used to
generate a result tree fragment that contains a number that is 1 greater
than a number passed into it as a parameter. When assigned to a variable,
this fragment can be treated like a number in numeric comparisons.

For generating numeric text nodes based on things in the source tree, you
can use xsl:count.

   - Mike
Mike J. Brown, software engineer at         My XML/XSL resources: in Denver, Colorado, USA 

 XSL-List info and archive:

Current Thread