Add row number : table « XSLT stylesheet « XML






Add row number


File: Data.xml 

<?xml version="1.0"?>
<books>
  <book category="reference">
      <author>author1</author>
      <title>title 1</title>
      <price>8.95</price>
   </book>
   <book category="fiction">
      <author>author 2</author>
      <title>title 2</title>
      <price>12.99</price>
   </book>
   <book category="fiction">
      <author>author 3</author>
      <title>title 3</title>
      <price>8.99</price>
   </book>
   <book category="fiction">
      <author>J. R. R. Tolkien</author>
      <title>The Lord of the Rings</title>
      <price>22.99</price>
   </book>
</books>


File: Transform.xslt
<?xml version="1.0" encoding="iso-8859-1"?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:template match="books">
    <html>
      <body>
        <h1>A list of books</h1>
        <table width="640">
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="book">
    <tr>
      <td>
        <xsl:number />
      </td>
      <td>
        <xsl:value-of select="author" />
      </td>
      <td>
        <xsl:value-of select="title" />
      </td>
      <td>
        <xsl:value-of select="price" />
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
Output:

<html>
   <body>
      <h1>A list of books</h1>
      <table width="640">
         
         <tr>
            <td>1</td>
            <td>author1</td>
            <td>title 1</td>
            <td>8.95</td>
         </tr>
         
         <tr>
            <td>2</td>
            <td>author 2</td>
            <td>title 2</td>
            <td>12.99</td>
         </tr>
         
         <tr>
            <td>3</td>
            <td>author 3</td>
            <td>title 3</td>
            <td>8.99</td>
         </tr>
         
         <tr>
            <td>4</td>
            <td>J. R. R. Tolkien</td>
            <td>The Lord of the Rings</td>
            <td>22.99</td>
         </tr>
         
      </table>
   </body>
</html>

 








Related examples in the same category

1.Use xslt style sheet to output data in a table
2.Output to a table
3.for-each loop and table output
4.Sort a column
5.Use for-each to output table rows
6.select value for table cell
7.Get value with value-of for table cell
8.Use for-each to loop through nodes in certain level
9.Fill more one value into table cell
10.use
to format value in a table cell
11.Create table header and content in separated templates
12.One template per table row
13.Create a table with sorting
14.number column
15.Generate two tables
16.Create table header
17.Sort first then output to table