Ashish Banker
Ashish Banker

Reputation: 2473

Increment counter variable in xsl template call in for each loop

This is my XMl: I want to print sth like this ,Numbering as shown 1. 2. only if "name" is ABC . I am having little difficulty in assigning a counter variable and incrementing it and then checking for a certain value in XSLT. Here is my code:

<Details>
<name>ABC</name>
<EFFECTIVEDATE>2010-04-30</EFFECTIVEDATE>
</Details>
<Details>
<name>EFG</name>
<EFFECTIVEDATE>2010-04-30</EFFECTIVEDATE>
</Details>
<Details>
<name>XYZ</name>
<EFFECTIVEDATE>2022-04-01</EFFECTIVEDATE>
</Details>
<Details>
<name>ABC</name>
<EFFECTIVEDATE>2022-04-01</EFFECTIVEDATE>
</Details>
<Details>
<name>ABC</name>
<EFFECTIVEDATE>2022-04-01</EFFECTIVEDATE>
</Details>

Here is XSL stylesheet:

<xsl:for-each select="Details">
    </xsl:call-template name="DetaimTemplate">
</xsl:for-each>

<xsl:template name="DetaimTemplate">

</xsl:template> 

Expected result

  1.ABC
  2.ABC
  3.ABC

How can i print like 1. 2. and so on

Upvotes: 1

Views: 2702

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116992

Well, you could do simply:

<xsl:for-each select="Details[name='ABC']">
   <xsl:value-of select="position()" />
   <xsl:text>. ABC&#10;</xsl:text>
</xsl:for-each>

Of course, you will also need a well-formed input with a root element and a template matching that element to contain the above, before it can work.

Upvotes: 1

Saurav
Saurav

Reputation: 640

You haven't mentioned whether your output will be text or xml. I have provided the solution assuming you are expecting xml output. I have started from the point where your xslt parser is traversing over each of Details node

  <xsl:template match="Details">
    <xsl:if test="name='ABC'">
      <t>
        <xsl:value-of select="count(preceding-sibling::*[name='ABC']) + 1"/>
        <xsl:text>.ABC</xsl:text>
      </t>
    </xsl:if>
  </xsl:template>

Upvotes: 0

Related Questions