XSLT <xsl:for-each> Element

The XSLT <xsl:for-each> element is used to apply a template repeatedly for each node.

   select = Expression>  

Parameter explanation

Select: XPath Expression to be evaluated in current context to determine the set of nodes to be iterated.

XSLT Element Example

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


<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
   <employee id = "001">
   <employee id = "024"> 
   <employee id = "056"> 


<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">   
   <xsl:template match = "/"> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
               <!-- for-each processing instruction 
               Looks for each element matching the XPath expression 
               <xsl:for-each select="class/employee"> 
                        <!-- value-of processing instruction 
                        process the value of the element matching the XPath expression 
                        <xsl:value-of select = "@id"/> 
                     <td><xsl:value-of select = "firstname"/></td> 
                     <td><xsl:value-of select = "lastname"/></td> 
                     <td><xsl:value-of select = "nickname"/></td> 
                     <td><xsl:value-of select = "salary"/></td>   


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