1

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

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
Ashish Banker
  • 2,243
  • 3
  • 20
  • 21
  • 1
    Are you transforming your XML to plain text or to HTML or to another XML format? If you only want to number the `Details` elements with `name` as `ABC`, do you want to output the other `Details` elements as well, only without numbers? Or do you want to remove them? – Martin Honnen Jul 25 '15 at 14:42
  • Please see http://stackoverflow.com/questions/9608432/incrementing-and-checking-the-counter-variable-in-xslt (and other questions found by searching for "counter variables in XSLT" – Michael Kay Jul 25 '15 at 18:41

2 Answers2

1

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.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

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>
Saurav
  • 592
  • 4
  • 21