www.webdeveloper.com
Results 1 to 5 of 5

Thread: xsl param

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    2

    xsl param

    Hi, I am having difficulty displaying a variable to represent the percentage of students who receive a particular grade.
    Any help would be greatly appreciated my code is as follows:

    XML:
    <student>
    <first-name>Ulne</first-name>
    <last-name>Douglas</last-name>
    <grade>C</grade>
    </student>
    <student>
    <first-name>Ambrose</first-name>
    <last-name>Tyler</last-name>
    <grade>B</grade>
    </student>
    <student>
    <first-name>Roger</first-name>
    <last-name>Clemens</last-name>
    <grade>A</grade>
    </student>
    <student>
    <first-name>Monroe</first-name>
    <last-name>Hamburger</last-name>
    <grade>A</grade>
    </student>
    </class>

    XSL:
    <?xml version="1.0" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0" />

    <xsl:template name="print_grade">
    <xsl:param name="target_grade" select="''" />
    <xsl:variable name="grade" select="class/student[starts-with(grade,$target_grade)]" />
    <xsl:if test="$target_grade=''" />
    <h1><xsl:value-of select="$target_grade" /></h1>

    <!--
    Number of students receiving grade: <b><xsl:value-of select="count($grade)" /></b>

    Percentage of students receiving grade: <!--I need to print the percentage of students receiving a particular grade variable out of the total students-->
    -->
    </xsl:template>

    <xsl:template match="/">
    <html>
    <head>
    <title>Grade Report</title>
    </head>
    <body>
    <p>
    <xsl:call-template name="print_grade">
    <xsl:with-param name="target_grade" select="'A'"/>
    </xsl:call-template>
    </p>
    <p>
    <xsl:call-template name="print_grade">
    <xsl:with-param name="target_grade" select="'B'"/>
    </xsl:call-template>
    </p>
    <p>
    <xsl:call-template name="print_grade">
    <xsl:with-param name="target_grade" select="'C'"/>
    </xsl:call-template>
    </p>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    Last edited by spyro; 05-25-2009 at 05:31 PM.

  2. #2
    Join Date
    Feb 2009
    Posts
    33
    Code:
    <xsl:value-of select="concat(count(class/student[grade = $target_grade]) div count(class/student)  * 100,'%')"/>
    Cheers, John Bampton

  3. #3
    Join Date
    Feb 2009
    Posts
    33
    I would use XSLT 2.0 and do something like the following
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          exclude-result-prefixes="xs"
          version="2.0">
    	<xsl:template match="/">
    		<html>
    			<head>
    				<title>Grade Report</title>
    			</head>
    			<body>
    				<p>
    					<xsl:for-each-group select="class/student" group-by="grade">
    						<xsl:sort select="grade"></xsl:sort>
    						<xsl:variable name="currentgrade" select="grade"></xsl:variable>
    						<h1>	
    							<xsl:text> number of students receiving grade </xsl:text>
    							<xsl:value-of select="grade"></xsl:value-of>
    							<xsl:text> equals </xsl:text>
    							<xsl:value-of select="count(/class/student[grade = $currentgrade]) "></xsl:value-of>
    							<xsl:text> </xsl:text>
    							<xsl:text>Percentage </xsl:text>
    							<xsl:value-of select="concat(count(/class/student[grade = $currentgrade]) div count(/class/student) * 100,'%')"></xsl:value-of>
    						</h1>	
    						<br/>
    					</xsl:for-each-group>
    				</p>
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>
    This automatically picks up the grades without having to hard code them in the xsl in the call template.

    Cheers, John Bampton.

  4. #4
    Join Date
    Feb 2009
    Posts
    33
    Or you cound use XSL 1.0 with the following:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    	<xsl:template match="/">
    		<html>
    			<head>
    				<title>Grade Report</title>
    			</head>
    			<body>
    				<p>
    					<xsl:for-each select="class/student[not(grade = preceding::grade)]" >
    						<xsl:sort select="grade"></xsl:sort>
    						<xsl:variable name="currentgrade" select="grade"></xsl:variable>
    						<h1>	
    							<xsl:text> number of students receiving grade </xsl:text>
    							<xsl:value-of select="grade"></xsl:value-of>
    							<xsl:text> equals </xsl:text>
    							<xsl:value-of select="count(/class/student[grade = $currentgrade]) "></xsl:value-of>
    							<xsl:text> </xsl:text>
    							<xsl:text>Percentage </xsl:text>
    							<xsl:value-of select="concat(count(/class/student[grade = $currentgrade]) div count(/class/student) * 100,'%')"></xsl:value-of>
    						</h1>	
    						<br/>
    					</xsl:for-each>
    				</p>
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>
    Regards, John Bampton.

  5. #5
    Join Date
    May 2009
    Posts
    2
    Thanks John i have adapted your solution and it works perfectly:

    <xsl:variable name="percent" select="count($grade)" />

    Percentage of students receiving grade:
    <b><xsl:value-of select="number($percent) div count(//grade) * 100" />%</b>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.34159 seconds
  • Memory Usage 3,012KB
  • Queries Executed 13 (?)
More Information
Template Usage (30):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates