XSLT Template

XSLT <xsl:template> Element

A single or multiple set of rules are present in an XSL style sheet, also called templates. The rules to apply when a specified node is matched, is included in a template.

The <xsl:template> Element:

To build templates, the <xsl:template> element is used and to associate a template with an XML element, the match attribute is used. Again, to define a template for the entire XML document, the match attribute can be used. An XPath expression makes the value of the match attribute, i.e., match=”/” defines the whole document.

Example:

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2020
    300.00
  


XSLT Code:




 

  

List of Books

Title Price
. .

Output:

Explanation:

Being an XML document, an XSL stylesheet always begins with the XML declaration, i.e., <?xml version=”1.0″ encoding=”UTF-8″?>. Now, to specify that a document is an XSLT stylesheet document along with the version number and XSLT namespace attributes, the <xsl:stylesheet> is used. The template is defined using the <xsl:template> element, while to associate the template with the root of the XML source document, the match=”/” attribute is used. Again, to specify some HTML to write to the output, the content inside the <xsl:template> element takes the job. The end of the template and the end of the style sheet is marked by the last two lines.