A couple of points that will hopefully help in the future:
<xsl:key name="course-codes" match="course" use="code" />
<xsl:key name="courses-by-CODE" match="course" use="code" />
The above two keys are identical, so one is unneeded.
Code:
<xsl:for-each select="$course-codes">
<xsl:variable name="courses" select="key('courses-by-CODE', .)" />
<tr>
<td bgcolor="#cdd8fe"><xsl:value-of select="../name" /></td>
<xsl:for-each select="$course-codes">
The above code loops through $course-codes twice, so this is obviously not going to produce the produce the correct output. I think the second loop should have been "$courses".
However you then have the problem that each course is printed out with no regard for the year. As the number of years appears to be fixed at 5 I simply replaced the <xsl:for-each> with 5 <xsl:value-of> lines as below:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:key name="course-codes" match="course" use="code" />
<xsl:variable name="course-codes" select="/ugcourses/course[generate-id() = generate-id(key('course-codes', code)[1])]/code" />
<xsl:template match="/ugcourses">
<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr>
<th bgcolor="#eeeeee"><div align="left">Course Name</div></th>
<th bgcolor="#eeeeee"><div align="center">First Year</div></th>
<th bgcolor="#eeeeee"><div align="center">Second Year</div></th>
<th bgcolor="#eeeeee"><div align="center">Third Year</div></th>
<th bgcolor="#eeeeee"><div align="center">Fourth Year</div></th>
<th bgcolor="#eeeeee"><div align="center">Fifth Year</div></th>
</tr>
<xsl:for-each select="$course-codes">
<xsl:variable name="courses" select="key('course-codes', .)" />
<tr>
<td bgcolor="#cdd8fe"><xsl:value-of select="../name" /></td>
<td bgcolor="#cdd8fe"><xsl:value-of select="$courses[year = 1]" /></td>
<td bgcolor="#cdd8fe"><xsl:value-of select="$courses[year = 2]" /></td>
<td bgcolor="#cdd8fe"><xsl:value-of select="$courses[year = 3]" /></td>
<td bgcolor="#cdd8fe"><xsl:value-of select="$courses[year = 4]" /></td>
<td bgcolor="#cdd8fe"><xsl:value-of select="$courses[year = 5]" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
/- Sam Judson : Wrox Technical Editor -/