Jason
Jason

Reputation: 399

How to divide with XSL - but the way I am trying to do this is a little tricky

So i found out how to add up values in XSL, the following link will show you what I done to get it.

How to use the count() function is XSL - trying to count the amount of "A"s there are in a report

But now I want find out the percentage each report value has.

So, what I mean is, there's 8 total reports, but 'A' only has 4 of them, so that means 'A' has %50 of the total report values.

And 'B' has 3, so that means it has %37.5 of the total report values and so on.

How do I do this?

I figure I get the number of each report value, like 'A' which is 4 - then i need to use the count() function to count the total number of reports, and divide 'A' by the total value.

Im lost on how to get this done.

I know that this gets the total number of 'A's.

<xsl:value-of select="count(/class/student[grade='A'])"/> 

And this gets the total number of reports.

<xsl:value-of select="count(/class/student/grade)"/>

But I dont know how to get the first value and divide it by the second * 100. I havnt figured out a way to give each value a name or ID so I can reference them - im completely lost.

Upvotes: 1

Views: 12569

Answers (1)

porges
porges

Reputation: 30580

You could just do division inline:

<xsl:value-of select="count(/class/student[grade='A']) div count(/class/student/grade)"/> 

However, this is a bit messy. You can tidy it up like this:

<xsl:variable name="students" select="/class/student"/>
<xsl:variable name="gradeAStudents" select="$students[grade='A']"/>
<xsl:variable name="gradeBStudents" select="$students[grade='B']"/>
<!-- etc -->

<xsl:variable name="proportionGradeA" select="count($gradeAStudents) div count($students)"/>
<xsl:variable name="proportionGradeB" select="count($gradeBStudents) div count($students)"/>
<!-- etc -->

<!-- then you can use this somewhere else to display the result -->

<xsl:value-of select="$proportionGradeA"/>

Upvotes: 5

Related Questions