Working with Expressions, Variables and xsl:for-each in XSLT - Working with xsl:for-each in XSLT
(Page 3 of 6 )
Let us consider that I would like to display all the employee details in the form of a table without using templates.
The following is a simple solution to bring this about:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table width="50%" cellspacing="0" cellpadding="0" style="font-
family:verdana;font-size:X-Small" border="1">
<tr bgcolor="#336699">
<th align="left">
<font color="White">ID</font>
</th>
<th align="right">
<font color="White">Name</font>
</th>
<th align="right">
<font color="White">Salary</font>
</th>
<th align="right">
<font color="White">Department</font>
</th>
</tr>
<xsl:for-each select="SQLData/Rows">
<tr>
<td align="left">
<xsl:value-of select="Empno" />
</td>
<td align="right">
<xsl:value-of select="Ename" />
</td>
<td align="right">
<xsl:value-of select="Sal" />
</td>
<td align="right">
<xsl:value-
of select="Deptno" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
When the above code gets executed, you are likely to get the following transformation:
<table width="50%" cellspacing="0" cellpadding="0" style="font-
family:verdana;font-size:X-Small" border="1">
<tr bgcolor="#336699">
<th align="left">
<font color="White">ID</font>
</th>
<th align="right">
<font color="White">Name</font>
</th>
<th align="right">
<font color="White">Salary</font>
</th>
<th align="right">
<font color="White">Department</font>
</th>
</tr>
<tr>
<td align="left">1001</td>
<td align="right">Jag</td>
<td align="right">4400</td>
<td align="right">10</td>
</tr>
.
.
.
</table>
The most important construct from the above XSLT is the following:
<xsl:for-each select="SQLData/Rows">
.
.
</xsl:for-each>
The above construct automatically iterates for every presence of “SQLData/Rows.” The execution of that construct gets completed only if no more “SQLData/Rows” are available for processing. We can even nest “xsl:for-each” constructs according to our requirements.
Next: Working with xsl:for-each along with xsl:choose and xsl:template >>
More ASP.NET Articles
More By Jagadish Chaterjee