Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:32:01+00:00 2026-06-03T23:32:01+00:00

I have been trying to figure out why my variable assigned sum isn’t working,

  • 0

I have been trying to figure out why my variable assigned sum isn’t working, also my table output is repeating only the first element through out the whole table. What i am trying to get this code to do is get the studentID of each student printed in the first column, the student name of that ID in the second column, and then assign a variable that holds the students total mark of the 3 assessments they have completed to print their total mark in the third column, followed by as assignment of HD, D, C, P, or F as based on their total mark e.g. HD is 85 plus and D is 75+ but not higher than 84. etc.

Can someone tell me where I am going wrong? I am still new to XML/XSL so criticism is welcome.

grade.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

    <xsl:variable name="StudentAmount" select="count(document('AssessmentItems.xml')/assessmentList/unit/studentList/student)"/>
    <xsl:variable name="totalmark" select="sum(document('AssessmentItems.xml')/assessmentList/unit/*
    [//assessmentList/unit/assessmentItems/assessment/@studId = //assessmentList/unit/studentList/student/@sid])"/>

  <html>
  <body>
    <h2>Grade Report for <xsl:value-of select="assessmentList/unit/@unitId"/> - <xsl:value-of select="assessmentList/unit/unitName"/></h2>
    <p>Number of students in this unit: <xsl:value-of select="$StudentAmount"/></p>

    <table border="1">
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Total Mark</th>
        <th>Grade</th>
      </tr>
      <xsl:for-each select="assessmentList/unit/studentList/student">
      <tr>
        <td><xsl:value-of select="document('AssessmentItems.xml')/assessmentList/unit/studentList/student/@sid"/></td>
        <td><xsl:value-of select="document('AssessmentItems.xml')/assessmentList/unit/studentList/student"/></td>
        <td><xsl:value-of select="document('AssessmentItems.xml')/assessmentList/unit/assessmentItems/assessment/mark"/></td>

        <xsl:choose>
        <xsl:when test="$totalmark &gt; 85">
        <td color="blue">HD</td>
        </xsl:when>

        <xsl:when test="$totalmark &gt; 75">
        <td color="black">D</td>
        </xsl:when>

        <xsl:when test="$totalmark &gt; 65">
        <td color="black">C</td>
        </xsl:when>

        <xsl:when test="$totalmark &gt; 50">
        <td color="black">P</td>
        </xsl:when>

        <xsl:otherwise>
        <td color="red">F</td>
        </xsl:otherwise>
        </xsl:choose>

      </tr>
      </xsl:for-each>
    </table>

    </body>
  </html>
</xsl:template>
</xsl:stylesheet>

and this is the file AssessmentItems.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="grade.xsl"?>
<assessmentList>
    <unit unitId="3311">
        <unitName>Learn To Read</unitName>
        <studentList>
            <student sid="1001">Lisa Simpson</student>
            <student sid="1002">Barney Rubble</student>
            <student sid="1003">Donald Duck</student>
        </studentList>
        <assessmentItems>
            <assessment name="Assignment 1" weight="20">
                <mark studId="1001">12</mark>
                <mark studId="1002">18</mark>
                <mark studId="1003">9</mark>
            </assessment>
            <assessment name="Assignment 2" weight="25">
                <mark studId="1001">23</mark>
                <mark studId="1002">14</mark>
                <mark studId="1003">12.5</mark>
            </assessment>
            <assessment name="Quiz" weight="15">
                <mark studId="1001">13</mark>
                <mark studId="1002">9</mark>
                <mark studId="1003">6</mark>
            </assessment>
            <assessment name="Final Exam" weight="40">
                <mark studId="1001">38</mark>
                <mark studId="1002">21</mark>
                <mark studId="1003">20.5</mark>
            </assessment>
        </assessmentItems>
    </unit>
</assessmentList>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:32:02+00:00Added an answer on June 3, 2026 at 11:32 pm

    Firstly, because you are only working on a single XML document, you don’t need the constant references to document(‘AssessmentItems.xml’) at all. So, for example

    <xsl:value-of 
       select="document('AssessmentItems.xml')/assessmentList/unit/studentList/student/@sid"/>
    

    Can be replaced with just

    <xsl:value-of select="/assessmentList/unit/studentList/student/@sid"/>
    

    This leads on to the second problem. The xpath above is relative to the document element of the XML and will return the @sid of the very first student it finds, and no the @sid of the student you are currently positioned on. You can simply do this in your case

    <xsl:value-of select="@sid"/>
    

    Another issue is that you define the variable totalmarks at the top of the XSLT, when in fact it should be defined within the scope of your xsl:for-each so that it is specific for the current student

    <xsl:variable name="totalmark" select="sum(../../assessmentItems/assessment/mark[@studId = current()/@sid])" />
    

    Actually, it may be better to make use of a key here, to look up the results

    <xsl:key name="marks" match="mark" use="@studId" />
    

    And to get the total results for a student….

     <xsl:variable name="totalmark" select="sum(key('marks', @sid))" />
    

    One final comment, although not a problem, it is often better to use xsl:apply-templates rather than xsl:for-each as this avoids excessive indentation, and allows better code re-use.

    Try the following XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:key name="marks" match="mark" use="@studId"/>
    
       <xsl:template match="/">
          <xsl:variable name="StudentAmount" select="count(/assessmentList/unit/studentList/student)"/>
          <html>
             <body>
                <h2>Grade Report for 
                   <xsl:value-of select="assessmentList/unit/@unitId"/>- 
                   <xsl:value-of select="assessmentList/unit/unitName"/>
                </h2>
                <p>Number of students in this unit: 
                   <xsl:value-of select="$StudentAmount"/></p>
                <table border="1">
                   <tr>
                      <th>ID</th>
                      <th>Name</th>
                      <th>Total Mark</th>
                      <th>Grade</th>
                   </tr>
                   <xsl:apply-templates select="assessmentList/unit/studentList/student"/>
                </table>
             </body>
          </html>
       </xsl:template>
    
       <xsl:template match="student">
          <xsl:variable name="totalmark" select="sum(key('marks', @sid))"/>
          <tr>
             <td>
                <xsl:value-of select="@sid"/>
             </td>
             <td>
                <xsl:value-of select="."/>
             </td>
             <td>
                <xsl:value-of select="$totalmark"/>
             </td>
             <xsl:choose>
                <xsl:when test="$totalmark &gt; 85">
                   <td color="blue">HD</td>
                </xsl:when>
                <xsl:when test="$totalmark &gt; 75">
                   <td color="black">D</td>
                </xsl:when>
                <xsl:when test="$totalmark &gt; 65">
                   <td color="black">C</td>
                </xsl:when>
                <xsl:when test="$totalmark &gt; 50">
                   <td color="black">P</td>
                </xsl:when>
                <xsl:otherwise>
                   <td color="red">F</td>
                </xsl:otherwise>
             </xsl:choose>
          </tr>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your XML, the following HTML is output

    <html>
       <body>
          <h2>Grade Report for 3311- Learn To Read</h2>
          <p>Number of students in this unit: 3</p>
          <table border="1">
             <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Total Mark</th>
                <th>Grade</th>
             </tr>
             <tr>
                <td>1001</td>
                <td>Lisa Simpson</td>
                <td>86</td>
                <td color="blue">HD</td>
             </tr>
             <tr>
                <td>1002</td>
                <td>Barney Rubble</td>
                <td>62</td>
                <td color="black">P</td>
             </tr>
             <tr>
                <td>1003</td>
                <td>Donald Duck</td>
                <td>48</td>
                <td color="red">F</td>
             </tr>
          </table>
       </body>
    </html>
    

    Do note this assumes only one unit element in your XML. If your actual XML have multiple units, and you wanted a separate table for each, then this is not a problem, you would just need to make sure the unit id is part of the xsl:key so you can look up results for a given student in a given unit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going mad trying to figure out why my scripts weren't working,
I have been trying to figure out a solution but nothing has really presented
I have been trying to figure out a way to tag several methods from
I have been trying to figure out why and how to use performSelector. I
I have been trying to figure this out for way to long tonight. I
I have been trying to figure this out for some time. It seems lots
I have been pulling my hair out trying to figure out what I can't
I have been scratching my head for hours trying to figure out why this
I have been pounding my head against a wall trying to figure out what
Here's a fun one I've been trying to figure out. I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.