Solved

Output a grade report using XML and XSLT

Posted on 2011-03-05
3
944 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now