troubleshooting Question

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

Avatar of Samm1502
Samm1502 asked on
Web Languages and StandardsXML
3 Comments1 Solution435 ViewsLast Modified:
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"/>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros