Avatar of Samm1502
Samm1502
 asked on

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

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

Web Languages and StandardsXML

Avatar of undefined
Last Comment
zc2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
zc2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Samm1502

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

Cheers
S
zc2

You're welcome
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck