xsl:template Element in XSLT

By Vijai Anand Blogs | XML Apr 15, 2012
In this blog you will see an example for <xsl:template> Element in XSLT.

<xsl:template> Element : 

A template contains rules which will be applied when a specifiied node is matched.

Employees.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <
Employee>
    <
Name>Vijai</Name>
    <Id>1</Id>
    <Designation>Associate</Designation>
    <Location>Bangalore</Location>
    <Department>SharePoint</Department>
    <Experience>3</Experience>
  </Employee>
  <
Employee>
    <
Name>Ranjith</Name>
    <Id>2</Id>
    <Designation>Associate</Designation>
    <Location>Bangalore</Location>
    <Department>SharePoint</Department>
    <Experience>5</Experience>
  </Employee>
  <
Employee>
    <
Name>Rakesh</Name>
    <Id>3</Id>
    <Designation>Associate</Designation>
    <Location>Chennai</Location>
    <Department>LN</Department>
    <Experience>6</Experience>
  </Employee>
  <
Employee>
    <
Name>Kavya</Name>
    <Id>4</Id>
    <Designation>Associate</Designation>
    <Location>Chennai</Location>
    <Department>LN</Department>
    <Experience>3</Experience>
  </Employee>
  <
Employee>
    <
Name>Pai</Name>
    <Id>5</Id>
    <Designation>Associate</Designation>
    <Location>Chennai</Location>
    <Department>LN</Department>
    <Experience>3</Experience>
  </Employee>
</
Employees>


EmployeesXSL.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Employees/Employee">
    <div STYLE="font-weight:bold; color:red;font-style:italic">
      <xsl:value-of select="Name"/>
    </div>
  </
xsl:template>
</
xsl:stylesheet>

Output:

Vijai
Ranjith
Rakesh
Kavya
Pai

Explanation: 

--XML declaration
<?xml version="1.0" encoding="utf-8"?> 

--Style Sheet declaration
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

--defines the template element. When Employees/Employee xml element is matched the rules will be applied and the output will be displayed 
<xsl:template match="Employees/Employee"> 

-- <div> tag with STYLE attribute is used within which the output will be displayed 
<div STYLE="font-weight:bold; color:red;font-style:italic"> 

--displays only the Name value from the xml 
<xsl:value-of select="Name"/> 

--Closing the <div> element
</div>

--Closing the <xsl:template> element 
</xsl:template>
 
--Closing the <xsl:stylesheet> element
</xsl:stylesheet>

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs