Re: [xsl] how to increment a variable in a for-each loop

Subject: Re: [xsl] how to increment a variable in a for-each loop
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 23 Jan 2002 18:43:09 -0500
Yan,

Your xsl:for-each is throwing you off. It's just selecting the current node; but since it is, position() is always 1.

Try it without. If position() is still getting thrown off (which could be happening if you are processing nodes you haven't accounted for -- for example, whitespace-only text nodes in your source which you can only see if you know how to look for them), then try using, instead,

count(preceding-sibling::*)

which is more robust than position() for these purposes; or alternatively make sure the apply-templates that is getting you this template is constructed to select just the nodes you want (probably the best solution).

If this is unclear, we can explain it better if we see both some source data, and the template(s) one step "above" this template (i.e. where elements are selected to which this template matches -- i.e. the one that says mode="diff_cols"). Concrete examples make discussion easier. :-)

Good luck,
Wendell

At 06:14 PM 1/23/02, you wrote:
I have a code snippet as below:
<xsl:template match="*" mode="diff_cols">
<xsl:for-each select=".">
    <xsl:if test="position() mod 2 = 1">
        <fo:table-cell  background-color="red">
<fo:block><xsl:value-of select="."/></fo:block>
        </fo:table-cell>
    </xsl:if>
    <xsl:if test="position() mod 2 = 0">
        <fo:table-cell  background-color="blue">
        <fo:block><xsl:value-of
select="."/></fo:block>
        </fo:table-cell>
    </xsl:if>
    </xsl:for-each>
</xsl:template>

Unfortunately, it did not work.  The background color
in all columns is red.

Can you help to debug?


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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



Current Thread