Solved

Output a grade report using XML and XSLT

Posted on 2011-03-05
3
951 Views
Last Modified: 2013-11-18
I'm trying to create a grade report to summarize class grades on an assignment.

So far I've figured out a recursion to display each student and their grade, but now I want a summary for each grade.

Attached is the xml and the xslt I'm starting from. I need to modify the xslt to output a summary like the image for each grade.
<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- Modify this part   
 <xsl:template name="print_grade" >
        <xsl:param name="list" />
        <xsl:param name="target_grade" />  
        <xsl:value-of select="$target_grade" />
        <xsl:if test="$list">
            <tr>
                <td><xsl:value-of select="$list/first-name" /></td>
                <td><xsl:value-of select="$list/last-name" /></td>
                <td><xsl:value-of select="$list/grade" /></td>
            </tr>
            <xsl:call-template name="print_grade">
                <xsl:with-param name="target_grade" select="$target_grade"/>
                <xsl:with-param name="list" select="$list/following-sibling::student[grade = $target_grade][1]"/>
            </xsl:call-template>
        </xsl:if>
        
    </xsl:template>      
    -->
    <!-- Keep this
    <xsl:template match="/">
        <html><head><title>Grade Report</title></head><body> 
            <table>
                <xsl:call-template name="print_grade">
                <xsl:with-param name="target_grade" select="'A'"/>
                <xsl:with-param name="list" select="class"/>
            </xsl:call-template>
            </table>
            <p/>
                <table>
                    <xsl:call-template name="print_grade">
                <xsl:with-param name="target_grade" select="'B'"/>
                <xsl:with-param name="list" select="class"/>
            </xsl:call-template>
                </table>
                <p />
            <table>
                <xsl:call-template name="print_grade">
                    <xsl:with-param name="target_grade" select="'C'"/>
                    <xsl:with-param name="list" select="class"/>
                </xsl:call-template>
                
            </table>
            <p />
        </body></html> 
    </xsl:template>  
	-->
</xsl:stylesheet>

Open in new window

<?xml version="1.0"?>
<?xml-stylesheet href="summary.xsl" type="text/xsl"?>

<class>
	<student>
		<first-name>Jane</first-name>
		<last-name>Doe</last-name>
		<grade>B</grade>
	</student>
	<student>
		<first-name>John</first-name>
		<last-name>Smith</last-name>
		<grade>C</grade>
	</student>
	<student>
		<first-name>Harry</first-name>
		<last-name>Grandson</last-name>
		<grade>B</grade>
	</student>
	<student>
		<first-name>Lacy</first-name>
		<last-name>Jones</last-name>
		<grade>C</grade>
	</student>
	<student>
		<first-name>Ulne</first-name>
		<last-name>Douglas</last-name>
		<grade>A</grade>
	</student>
	<student>
		<first-name>Ambrose</first-name>
		<last-name>Tyler</last-name>
		<grade>C</grade>
	</student>
	<student>
		<first-name>Roger</first-name>
		<last-name>Clemens</last-name>
		<grade>B</grade>
	</student>
	<student>
		<first-name>Monroe</first-name>
		<last-name>Hamburger</last-name>
		<grade>B</grade>
	</student>
</class>

Open in new window

Summary.jpg
0
Comment
Question by:Thmstec
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
zc2 earned 500 total points
ID: 35044981
No, you don't need a recursion in here  (if only your assignment does not specify explicitly to use a recursion).
I modified your XSLT, it now gives up a decent output.

<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template name="print_grade" >
        <xsl:param name="list" />
        <xsl:param name="target_grade" />  
        <h1><xsl:value-of select="$target_grade" /></h1>
        <xsl:variable name="num" select="count($list/student[grade = $target_grade])"/>
        <p>
        Number of students receiving grade: <xsl:value-of select="$num"/><br/>
        Percentage of students receiving grade: <xsl:value-of select="$num * 100 div count($list/student)"/>
        </p>
        <xsl:if test="$list">
            <table>
    		<xsl:for-each select="$list/student[grade = $target_grade]">
	            <tr>
	                <td><xsl:value-of select="first-name" /></td>
	                <td><xsl:value-of select="last-name" /></td>
	                <td><xsl:value-of select="grade" /></td>
	            </tr>
      		</xsl:for-each>
            </table>
        </xsl:if>
    </xsl:template>      
    
    <xsl:template match="/">
        <html><head><title>Grade Report</title></head><body> 
                <xsl:call-template name="print_grade">
                <xsl:with-param name="target_grade" select="'A'"/>
                <xsl:with-param name="list" select="class"/>
            </xsl:call-template>
            <p/>
                    <xsl:call-template name="print_grade">
                <xsl:with-param name="target_grade" select="'B'"/>
                <xsl:with-param name="list" select="class"/>
            </xsl:call-template>
                <p />
                <xsl:call-template name="print_grade">
                    <xsl:with-param name="target_grade" select="'C'"/>
                    <xsl:with-param name="list" select="class"/>
                </xsl:call-template>
            <p />
        </body></html> 
    </xsl:template>  

</xsl:stylesheet>

Open in new window

0
 

Author Closing Comment

by:Thmstec
ID: 35044993
Thanks! This helps a lot. I'm trying to learn xml without even basic knowledge of html... all I know is MATLAB and Visual Basic. So I'm finding it to be about as challenging as drinking from a firehose.
0
 
LVL 18

Expert Comment

by:zc2
ID: 35045004
You're welcome! That's true, XSLT requires some kind of different thinking, but as soon you get used to it becomes a really powerful and elegant tool.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
website content maintenance 3 93
read an xml file in perl 2 31
Configure a Bean in an XML file 4 31
XML Document XPath with Namespaces 3 20
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question