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

Samm1502
Samm1502 used Ask the Experts™
on
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()"/>
        </xsl:call-template>
</xsl:for-each>

<!-- 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()"/>
	</xsl:call-template>
 /xsl:for-each>

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

Open in new window

Comment
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"/>

Author

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

Cheers
S
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