Solved

Output a grade report using XML and XSLT

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell XML in variable 4 43
asp Google Map 2 92
parsing xml using powershell 6 53
XSLT to pick out a particular element and then pick out all following siblings 4 20
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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. 
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

739 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