This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: A Counter Variable in XSLT?
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: Re: [xsl] A Counter Variable in XSLT?
- From: Jörg Heinicke <joerg dot heinicke at gmx dot de>
- Date: Fri, 5 Oct 2001 21:50:36 +0200
- References: <000101c14dcc$3e17d160$5f0f7097@sm00d0b75c7d7e.llu.edu>
- Reply-To: xsl-list at lists dot mulberrytech dot com
> Thanks folks for your replies.
>
> > But you need
> > to say what it is
> > you want to do before anyone can show the xslt equivalent)
> >
>
> What I want to do:
>
> My stylesheet consists of one template rule. When there is a
> match, I want a line in the output to say, for example, "Match #
> 123:", followed by relevant text. Since I don't know ahead of
> time how many matches there will be in the source document,
> somehow the stylesheet needs to keep a count. Something like:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> <xsl:output method="text"/>
> <xsl:template match="/*">
> <xsl:if test="@col">
> Match # n : <!-- n is the nth match, the thing that needs to
> be incremented -->
> Collection: "<xsl:value-of select="@col"/>"
> Content: <xsl:value-of select="."/>
> </xsl:if>
> </xsl:template>
> </xsl:stylesheet>
In your case you only need to count the nodes which are before the current
node:
<xsl:value-of select="count(preceding-sibling::*[@col]) + 1"/>
> > You need to unlearn procedural programming! XSLT is a
> > declarative,
> > functional language.
>
> I am willing! Just what is meant by "declarative, functional"?
>
> >3. If neither of options 1 or 2 is suitable, you can probably
> count what you
> >want by using a recursive named template.
>
> Just what is a "recursive named template"?
an example:
<xsl:template match="/">
<xsl:call-template name="count-down">
<xsl:with-param name="counter">10</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="count-down">
<xsl:param name="counter"/>
<xsl:value-of select="$counter"/>
<xsl:if test="$counter > 0">
<xsl:call-template name="count-down">
<xsl:with-param name="counter" select="$counter - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
> Thank you for your input,
>
> Grant Mc Auley
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list