How to keep a count of number of tags written so far?

Samm1502 used Ask the Experts™
I have a style sheet that loop through the original XML data twice.  The first time it picks up all the erroneaous records and writes empty <record> tags for them.  It then matches all the correct records and write data filled <record> tags for those.

What I need to do is to give each record tag an attribute called count assigned to which should be the integer value matching the count of record tags written so far.

I initially tried assigning position() to my count attribute but the problem with that is that I can end up with more than one record with the same integer value because the position value gets reset as the style sheet starts the second loop.

Is there a simple way to increment a counter every time a record is written no matter in which loop it happens so that each of my <record> tags gets an ascending sequential count attribute?

I have attached my XSLT as a code snippet and as a file if easier to read so you can see what my code is doing. Source.txt
<!-- Process invalid records -->
<xsl:for-each select="Row[generate-id() = generate-id(key('row-by-invalid-numbers-or-columns', concat(translate(string(Cell[7]/Data), $smallcase, $uppercase),Cell[3]/Data))[1])]">
 	<xsl:call-template name="WriteEmptyRecord">
          <xsl:with-param name="myposition" select="position()"/>

<!-- Process valid records -->
<xsl:for-each select="Row[generate-id() = generate-id(key('row-by-valid-numbers-and-columns', concat(translate(string(Cell[7]/Data), $smallcase, $uppercase),Cell[3]/Data))[1])]">
        <xsl:call-template name="WritePopulatedRecord">
	  <xsl:with-param name="myposition" select="position()"/>

<!-- The attribute is added as follows...-->
    <xsl:attribute name="Count">
      <xsl:value-of select="$myposition"/>

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what if just count the empty records to a variable:

<xsl:variable name="NumEmpty" select="count(Row[generate-id() = generate-id(key('row-by-invalid-numbers-or-columns', concat(translate(string(Cell[7]/Data), $smallcase, $uppercase),Cell[3]/Data))[1])])"/>

then use it in the following counting:

<xsl:with-param name="myposition" select="position() + $NumEmpty"/>


Brilliant thanks and so simple - always looking for the complex solutions!

You're welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial