Mega Code Archive

 
Categories / XML / XSLT StyleSheet
 

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>