xml 如何使用 count() 函数是 XSL - 尝试计算报告中“A”的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6092687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:43:22  来源:igfitidea点击:

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

xmlxslt

提问by Jason

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

我正在尝试计算A学校报告中's的数量。

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?

我如何获得报告中的数量A

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:

但这很重要 - 所以我试图只得到A's :

<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?

但这也行不通——你们怎么看?

回答by jelovirt

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

回答by Codename UKS

You can also use:

您还可以使用:

count(/class/student/grade[text()="A"])