Accessing variables outside of for-each in XSLT


I am having issues at the moment with the scope of certain variables within my XSLT document, here is the section of code in question:

<xsl:for-each select="all/courses/allcourses/course"> <a> <xsl:attribute name="href">javascript:void(0)</xsl:attribute> <xsl:attribute name="onclick">javascript:transform('<xsl:value-of select="c_code"/>');</xsl:attribute> <xsl:value-of select="c_title"/> </a> <br/> <br/> <course code="{c_code}"> <strong> Enrolled: </strong> <xsl:variable name="courseCreditPoints" select="coursecp"/> <xsl:variable name="numberOfEnrolled" select="count(student[sum(results/u_points)!= $courseCreditPoints])"/> <xsl:value-of select="$numberOfEnrolled"/> <br/> <strong> Graduated: </strong> <xsl:value-of select="count(student[sum(results/u_points) = $courseCreditPoints])"/> <br/> <xsl:variable name="numberOfGraduates" select="count(student[sum(results/u_points) = $courseCreditPoints])"/> </course> </xsl:for-each> <br/> <strong> Total number of students: </strong> <xsl:variable name="totalStudents" select="count(//studentID)"/> <xsl:value-of select="$totalStudents"/> <strong> Total enrolled students: </strong>

<strong>What I am trying to achieve:</strong> I am trying to access the variables within the for-each loop outside of it. What I want to be able to do is subtract the $numberOfGraduates variables from the variable $totalStudents (I can access this one, as it is declared outside of the loop) but I cannot do so as $numberOfGraduates is declared within the loop. I have tried just subtracting it within the loop, which works, but then it will print out the result several times.

What is the best way to go about accessing these variables within the for-each loop?

Thanks guys!


Your variable numberOfGraduates is per course, it has to be calculated for-each course separately, as you did.

Variable totalStudents is one for all courses. One student can take two courses, be sure that you do not count one student twice:

<xsl:variable name="totalStudents" select="count(distinct-values(//studentID))"/>


  • jComoBox input only characters (no numbers)
  • Compiling statically libc C code and ASM code
  • How can I apply differnt styles when transforming data using json2html based on data value?
  • Implicit property animations do not work with CAReplicatorLayer?
  • Change the width of the JQM panels
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • Javascript (MVC) load image (byte array) from database
  • Submit a form with jQuery / Javascript without ignoring “required” tag
  • JavaScriptCore External Arrays
  • Send data from edittext to listview
  • google maps autocomplete bounces back already cleared text …odd…odd…odd
  • PayPal API Listener Website Payments Standard URI
  • Jquery Mobile pageLoading() Method how does it work?
  • Form tag not showing up in haml file
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • How to get latest version of a artifact on Bintray using JSONP
  • Hide HTML elements without javascript, only CSS
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Simulate click Geckofx vb,net
  • IE7 and TinyMCE with Plone
  • How to make jdk.nashorn.api.scripting.JSObject visible in plugin [duplicate]
  • How does document.ready work with angular element directives?
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Zurb Foundation _global.scss meta styles for js?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • jQuery ready not fired after rails link_to is clicked
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • Array.prototype.includes - not transformed with babel
  • HTML download movie download link
  • Updating server-side rendering client-side
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?