Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

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 do you guys think?

like image 570
Jason Avatar asked May 23 '11 03:05

Jason


People also ask

How do you iterate through XSLT?

You can format your XSLT stylesheet to go to a specific node, and then loop through the given node set. You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through.

What is for each in XSLT?

The <xsl:for-each> element allows you to do looping in XSLT.

What is XSLT format?

The Extensible Stylesheet Language Transformation (XSLT) standard specifies a language definition for XML data transformations. XSLT is used to transform XML documents into XHTML documents, or into other XML documents.


1 Answers

<xsl:value-of select="count(/class/student[grade='A'])"/>
like image 173
jelovirt Avatar answered Nov 10 '22 06:11

jelovirt