newbie needs some help with sorting

Here should go questions about transforming XML with XSLT and FOP.
lock
Posts: 6
Joined: Tue Aug 05, 2008 11:25 am

newbie needs some help with sorting

Post by lock »

Hi there. I am having a lot of trouble sorting. At the moment, I have just learnt how to write XML code, and self admittedly, my code is VERY messy, but I dont fully know any other proper ways of doing things.

Attached is the URL of my XML code.

Code: Select all

<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40">
<xsl:output indent="yes"/>

<xsl:template match="/">
<html>
<head>
<title>Page 7</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css"/>
</head>
<body>
<center>
<h3>Passing Students</h3>
</center>
<xsl:apply-templates select="units"/>
</body>
</html>
</xsl:template>

<xsl:template match="units">
<xsl:for-each select="unit" >
<BR/>
<DIV align="center">
<table border="0" width="560">
<tr>
<td align="left" colspan="2"><B><xsl:value-of select="unitCode"/></B></td>
</tr>
<tr>
<td align="left" width="99"><b>Unit Leader:</b></td>
<td align="left"><xsl:value-of select="unitLeader"/></td>
</tr>
</table>
</DIV>
<BR/>
<div align="center">
<table border="0" width="560">
<tr>
<td class="headingcenter" width="99">Student ID</td>
<td class="headingcenter" width="287">Name</td>
<td class="headingcenter" width="66">Final Mark</td>
<td class="headingcenter" width="66">Grade</td>
</tr>
</table>
</div>
<xsl:apply-templates select="class/studentMarks/student"/>
</xsl:for-each>

</xsl:template>

<xsl:template match="student">
<xsl:for-each select="." >
<xsl:apply-templates select="assignments"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="assignments">
<xsl:if test="count(lateAssignment) =1">
<xsl:if test="lateAssignment/assNo=1">
<xsl:variable name="total" select="(assignment[@assNo='1']-(assignment[@assNo='1']*(lateAssignment/penalty div 100))+assignment[@assNo='2']+unitTest) div 3"/>
<xsl:if test="$total >= 50">
<div align="center">
<table border="0" width="560">
<tr>
<td class="data" width="99"><xsl:value-of select="../../student/@id"/></td>
<td class="data" width="287"><xsl:value-of select="../surname"/>, <xsl:value-of select="../firstname"/></td>
<td class="data" width="66"><xsl:value-of select="format-number(($total),'##.##')"/>%
</td>
<td class="data" width="66">
<xsl:choose>
<xsl:when test="$total >= 80">HD</xsl:when>
<xsl:when test="$total >= 70 and $total < 79">D</xsl:when>
<xsl:when test="$total >= 60 and $total < 69">C</xsl:when>
<xsl:when test="$total >= 50 and $total < 59">P</xsl:when>
</xsl:choose>
</td>
</tr>
</table>
</div>
</xsl:if>
</xsl:if>
<xsl:if test="lateAssignment/assNo=2">
<xsl:variable name="total" select="(assignment[@assNo='2']-(assignment[@assNo='2']*(lateAssignment/penalty div 100))+assignment[@assNo='1']+unitTest) div 3"/>
<xsl:if test="$total >= 50">
<div align="center">
<table border="0" width="560">
<tr>
<td class="data" width="99"><xsl:value-of select="../../student/@id"/></td>
<td class="data" width="287"><xsl:value-of select="../surname"/>, <xsl:value-of select="../firstname"/></td>
<td class="data" width="66"><xsl:value-of select="format-number(($total),'##.##')"/>%
</td>
<td class="data" width="66">
<xsl:choose>
<xsl:when test="$total >= 80">HD</xsl:when>
<xsl:when test="$total >= 70 and $total < 79">D</xsl:when>
<xsl:when test="$total >= 60 and $total < 69">C</xsl:when>
<xsl:when test="$total >= 50 and $total < 59">P</xsl:when>
</xsl:choose>
</td>
</tr>
</table>
</div>
</xsl:if>
</xsl:if>
</xsl:if>

<xsl:if test="count(lateAssignment) =0">
<xsl:variable name="total" select="(assignment[@assNo='1']+assignment[@assNo='2']+unitTest) div 3"/>
<xsl:if test="$total >= 50">
<div align="center">
<table border="0" width="560">
<tr>
<td class="data" width="99"><xsl:value-of select="../../student/@id"/></td>
<td class="data" width="287"><xsl:value-of select="../surname"/>, <xsl:value-of select="../firstname"/></td>
<td class="data" width="66"><xsl:value-of select="format-number(($total),'##.##')"/>%
</td>
<td class="data" width="66">
<xsl:choose>
<xsl:when test="$total >= 80">HD</xsl:when>
<xsl:when test="$total >= 70 and $total < 79">D</xsl:when>
<xsl:when test="$total >= 60 and $total < 69">C</xsl:when>
<xsl:when test="$total >= 50 and $total < 59">P</xsl:when>
</xsl:choose>
</td>
</tr>
</table>
</div>
</xsl:if>
</xsl:if>

<xsl:if test="count(lateAssignment) =2">
<xsl:variable name="ass1" select="assignment[@assNo='1']-(assignment[@assNo='1']*(lateAssignment/penalty div 100))"/>
<xsl:variable name="ass2" select="assignment[@assNo='2']-(assignment[@assNo='2']*(lateAssignment/penalty div 100))"/>
<xsl:variable name="total" select="(($ass1+$ass2+unitTest) div 3)"/>
<xsl:if test="$total >= 50">
<div align="center">
<table border="0" width="560">
<tr>
<td class="data" width="99"><xsl:value-of select="../../student/@id"/></td>
<td class="data" width="287"><xsl:value-of select="../surname"/>, <xsl:value-of select="../firstname"/></td>
<td class="data" width="66"><xsl:value-of select="format-number(($total),'##.##')"/>%
</td>
<td class="data" width="66">
<xsl:choose>
<xsl:when test="$total >= 80">HD</xsl:when>
<xsl:when test="$total >= 70 and $total < 79">D</xsl:when>
<xsl:when test="$total >= 60 and $total < 69">C</xsl:when>
<xsl:when test="$total >= 50 and $total < 59">P</xsl:when>
</xsl:choose>
</td>
</tr>
</table>
</div>
</xsl:if>
</xsl:if>

</xsl:template>

</xsl:stylesheet>
I am wanting to sort the output based on the SURNAME. How would I go about this?

Help would be much appreciated!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: newbie needs some help with sorting

Post by george »

Hi,

I believe that adding a sort on the apply-templates on students should work:

Code: Select all


<xsl:apply-templates select="class/studentMarks/student">
<xsl:sort select="surname"/>
</xsl:apply-templates>
If you still have problems please consider posting also a sample XML input.

Best Regards,
George
George Cristian Bina
Post Reply