Solved

Output a grade report using XML and XSLT

Posted on 2011-03-05
3
955 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 80
parse convert xml feed to text (python) 2 90
XML SQL 8 27
Grunt script for Build Process 1 42
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

808 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