XSLT <xsl:template> Element

An XSL stylesheet is composed of one or more sets of rules called templates.

Each template contains the rules that are applied when a specified node is matched.

The <xsl:template> element

The <xsl:template> element is used to build templates.

match Attributes are used to associate XML elements with templates. The match attribute can also be used to define a template for the entire document. The value of the match attribute is an XPath expression (for example, match="/" defines the entire document).

Alright, let's take a look at the simplified version of the XSL file from the previous section:

<?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>My CD Collection</h2>
   <table border="1">
     <tr bgcolor="#9acd32">
       <th>Title</th>
       <th>Artist</th>
     </tr>
     <tr>
       <td>.</td>
       <td>.</td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

Code Explanation:

Since an XSL stylesheet itself is also an XML document, it always starts with an XML declaration:

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

The next element,<xsl:stylesheet>defines this document as an XSLT stylesheet document (including the version number and XSLT namespace attributes).

<xsl:template> An element defines a template, while match="/" Attributes associate this template with the root of the XML source document.

<xsl:template> element defines the HTML code that is written to the output result.

The last two lines define the end of the template and the end of the stylesheet.

The result of the above transformation is similar to this:

View XML File,View XSL File,View Result

The result of this example has a slight defect because the data has not been copied from the XML document to the output.

In the next section, you will learn how to use <xsl:value-of> Elements select values from XML elements.