codeCompiler77
codeCompiler77

Reputation: 526

XSLT Get Total Count while in the for loop

I'm in my for loop

<xsl:for-each select="Alerts/AlertItem">

and

<xsl:value-of select="position()" />

gets me the current position, the index. So each time it would give me 1,2 and 3 assuming there are 3 items.

While inside this for loop, how do i get the total count of 3? And not the index?

Before the for loop i can get it with:

<xsl:value-of select="count(descendant::AlertItem[Code='2'])" />

but inside the loop i have tried:

<xsl:value-of select="count(parent::AlertItem[Code='2'])" />
<xsl:value-of select="count(ancestor::AlertItem[Code='2'])" />
<xsl:value-of select="count(AlertItem[Code='2'])" />

All return 0

Upvotes: 0

Views: 501

Answers (2)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25054

Before the for-each instruction, define a variable with the desired information:

<xsl:variable name="total-count" select="count(Alerts/AlertItem)"/>
<xsl:for-each select="Alerts/Alertitem">
  <xsl:message>Now processing item <xsl:value-of select="position()"/>
    of <xsl:value-of select="$total-count"/>
   ...       
</xsl:for-each>

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117165

You could count preceding sibling (or preceding) nodes. Not sure why you need this, it's not very efficient.

Note also that the last() function will return the total count of the currently processed node set (overall, not depending on the position of the currently processed node).

Upvotes: 1

Related Questions