Mega Code Archive

 
Categories / XML / XSLT StyleSheet
 

Match among a list of target values

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> </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>       <xsl:apply-templates />     </tr>   </xsl:template>   <xsl:template match="author | title | price">     <td>       <xsl:value-of select="." />     </td>   </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>                 </table>    </body> </html>