Solved

Output a grade report using XML and XSLT

Posted on 2011-03-05
3
957 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
AL3 Files 4 48
How to redirect all users but me properly to another page with htaccess 2 150
Wordpress Hacked 1 70
Retreiving SOAP FAULT messages using classical ASP 14 28
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

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