Re: [xsl] incrementing a counter in XSL

Subject: Re: [xsl] incrementing a counter in XSL
From: Greg Faron <gfaron@xxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Apr 2002 13:12:49 -0600
At 12:05 PM 4/18/2002, you wrote:
I am selecting some nodes from an XML doc with an if statment in a for-each
loop.  Out of the set of nodes I am looping through, I am only selecting
some in the if statment.  Each of these nodes that is selected by the if
statement needs a sequence number.  Originally I was using :
  <xsl:for-each select="Contract/TermRdr">
    <xsl:if test="StatusCd='A' or StatusCd='F'">
      <SequenceNum fieldType="Short" elementType="field">
        <xsl:number value ="position()"/>
      </SequenceNum>
      ....
    </xsl:if>
  </xsl:for-each>

First, don't try to implement a counter. There's a different way to do it in the XSLT mindset. Assuming that the provided xsl:if is the only test that you perform for each run through the xsl:for-each loop (I assume this due to the position of the ellipses), you can select your exact nodes during the xsl:for-each and forget about the xsl:if.


  <xsl:for-each select="Contract/TermRdr[StatusCd='A' or StatusCd='F']">
    <SequenceNum fieldType="Short" elementType="field">
      <xsl:number value ="position()"/>
    </SequenceNum>
    ....
  </xsl:for-each>

position() gives the position of the current element in relation to the others of the selected node-set, not the absolute value from the document order.


Greg Faron Integre Technical Publishing Co.



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


Current Thread