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

2

18

I'm trying to count the amount of A's there are in a school report.

Here is the report:

<class>
  <student>
    <first-name>Jane</first-name>
    <last-name>Doe</last-name>
    <grade>A</grade>
  </student>
  <student>
    <first-name>John</first-name>
    <last-name>Smith</last-name>
    <grade>B</grade>
  </student>
  <student>
    <first-name>Harry</first-name>
    <last-name>Grandson</last-name>
    <grade>A</grade>
  </student>
  <student>
    <first-name>Lacy</first-name>
    <last-name>Jones</last-name>
    <grade>C</grade>
  </student>
</class>

How do I get the number of A's in the report?

I came up with:

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

But that counts everything - So I tried to get only the A's with this:

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

But this doesn't work either.

I also tried this:

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

But that doesn't work either - what is the problem?

Kazoo answered 23/5, 2011 at 3:58 Comment(0)
F
31
<xsl:value-of select="count(/class/student[grade='A'])"/>
Fireboat answered 23/5, 2011 at 4:35 Comment(0)
P
0

You can also use:

count(/class/student/grade[text()="A"])
Pathological answered 20/5, 2019 at 9:59 Comment(2)
please provide a complete answerHusking
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Aerobatics

© 2022 - 2024 — McMap. All rights reserved.