0

How to add the increment variable in xslt

i have table , with student details,

i want to Add S.No front of the Firstname ,

like

1 firstnamJoth LastNameJO LocaitonTexas 2 FirstMithul LastNameFig LocationArron

In xslt how to do this, i know , with xslt we can do , i dont know where to start,

here is my XML and XSLT code

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="studentlist.xsl"?>
<details>

        <student>
        <a>i want serial no here</a>
        <firstname>SURESH</firstname>
        <lastname>VENKAT</lastname>
        <dob>09-08-1987</dob>
        <location>AVADI</location>
    </student>
    <student>
        <a>i want serial no here</a>
        <firstname>BHARANIKUMAR</firstname>
        <lastname>SRINIVASAN</lastname>
        <dob>09-08-1984</dob>
        <location>VILLIVAKKAM</location>
    </student>



</details>


<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Student Details</h2>
  <table border="1">
    <tr bgcolor="green">
      <th>FIRSTNAME</th>
      <th>LASTNAME</th>
      <th>DOB</th>
      <th>LOCATION</th>
    </tr>


    <xsl:for-each select="details/student">
    <xsl:sort select="firstname"/>
   <tr>


      <td><xsl:value-of select="firstname"/></td>
      <td><xsl:value-of select="lastname"/></td>
      <td><xsl:value-of select="dob"/></td>
      <td><xsl:value-of select="location"/></td>
     </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
1
  • FYI, variables in XSLT don't vary, so there is no way to increment a variable (unless you count recursion...). But as you can see there are usually ways to get equivalent functionality. Commented Nov 24, 2010 at 4:00

1 Answer 1

4

With you current implementation you should be able to use:

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

within the xsl:for-each loop to output a number which will increase for every iteration.

Sign up to request clarification or add additional context in comments.

1 Comment

But if it is used with paging then in next page it again started with 1... How to solve this...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.