XSLT <xsl:apply-template> Element

The XSLT <xsl:apply-template> element is used to tell XSLT processor to find the appropriate template to apply according to the type and context of each selected node.

snippet
<xsl:apply-template 
   select = Expression 
   mode = QName>
</xsl:apply-template>

Parameter explanation

Index Name Description
1) select It is used to process nodes selected by XPath expressions from the list of all nodes and its children.
2) mode It is used to allow an element as specified by its qualified names to be processed multiple times, each time producing a different result.

XSLT <xsl:apply-template> Element Example

Let's take an example to create a list of <employee> element with its attribute "id" and its child <firstname>, <lastname>, <nickname>, and <salary> by iterating over each employee.

Employee.xml

snippet
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>

Employee.xsl

snippet
<?xml version = "1.0" encoding = "UTF-8"?> 
<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">   
   <xsl:template match = "/"> 
      <html> 
         <body> 
            <h2>Employees</h2> 
            <xsl:apply-templates select = "class/employee" /> 
         </body> 
      </html>
   </xsl:template>  

   <xsl:template match = "class/employee"> 
      <xsl:apply-templates select = "@id" /> 
      <xsl:apply-templates select = "firstname" /> 
      <xsl:apply-templates select = "lastname" /> 
      <xsl:apply-templates select = "nickname" /> 
      <xsl:apply-templates select = "salary" /> 
      <br /> 
   </xsl:template>  
   <xsl:template match = "@id"> 
      <span style = "font-size = 25px;"> 
         <xsl:value-of select = "." /> 
      </span> 
      
</xsl:template> <xsl:template match = "firstname"> First Name:<span style = "color:brown;"> <xsl:value-of select = "." /> </span> <br /> </xsl:template> <xsl:template match = "lastname"> Last Name:<span style = "color:green;"> <xsl:value-of select = "." /> </span> <br /> <</xsl:template> <xsl:template match = "nickname"> Nick Name:<span style = "color:blue;"> <xsl:value-of select = "." /> </span> <br /> </xsl:template> <xsl:template match = "salary"> Marks:<span style = "color:red;"> <xsl:value-of select = "." /> </span> <br /> </xsl:template> </xsl:stylesheet>

Output:

XSLT Xsl apply template element 1
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +