我试图在XPath的count函数中使用true或fale语句,但遇到了一些错误。到目前为止,这是我的代码:
<xsl:for-each select="all/courses/allcourses/course">
<xsl:for-each select="student">
<xsl:variable name="StudentTotalPoints" select="sum(results/u_points)" />
<xsl:value-of select="$StudentTotalPoints" />
<xsl:variable name="amountEnrolled" select="count((360 - $StudentTotalPoints) != 0)" />
<xsl:value-of select="$amountEnrolled" />
</xsl:for-each>
</xsl:for-each>
有问题的XML代码是:
<all>
<courses>
<allcourses>
<course>
<c_code>U65</c_code>
<c_title>Computer Science</c_title>
<coursecp>360</coursecp>
<student>
<studentID>10265654</studentID>
<fname>Sarah</fname>
<lname>Clarke</lname>
<results>
<u_title>Communicating in an IT Environment</u_title>
<u_code>CSG1132</u_code>
<u_points>15</u_points>
<result>65</result>
<grade>CR</grade>
</results>
<results>
<u_title>Programming Principles</u_title>
<u_code>CSP1150</u_code>
<u_points>15</u_points>
<result>45</result>
<grade>N</grade>
</results>
</student>
<student> *same structure* </student>
问题是在声明变量amountEnrolled的行上,当试图加载有问题的页面时我受到了这个错误的欢迎:
&#34; XSLT转换期间出错:预计XPath表达式将返回一个NodeSet。&#34;
很明显,计数功能并没有返回任何东西。最好的方法是什么?我觉得应该很简单,但绝对不是。我要求的结果是计算360 - $StudentTotalPoints does NOT equal to 0
的次数,我应该怎么做呢?
提前致谢!
答案 0 :(得分:2)
学生将有多个
<results> </results>
<student></student>
好的,那么如果我们采用以下示例输入:
<all>
<courses>
<allcourses>
<course>
<c_code>U65</c_code>
<student>
<studentID>1</studentID>
<results>
<u_points>10</u_points>
</results>
<results>
<u_points>20</u_points>
</results>
<results>
<u_points>30</u_points>
</results>
</student>
<student>
<studentID>2</studentID>
<results>
<u_points>40</u_points>
</results>
<results>
<u_points>50</u_points>
</results>
<results>
<u_points>360</u_points>
</results>
</student>
</course>
</allcourses>
</courses>
</all>
并应用此样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:for-each select="all/courses/allcourses/course">
<course code="{c_code}">
<xsl:for-each select="student">
<student id="{studentID}">
<totalPoints><xsl:value-of select="sum(results/u_points)" /></totalPoints>
<amountEnrolled><xsl:value-of select="count(results/u_points[.!=360])" /></amountEnrolled>
</student>
</xsl:for-each>
</course>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
我们将收到:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<course code="U65">
<student id="1">
<totalPoints>60</totalPoints>
<amountEnrolled>3</amountEnrolled>
</student>
<student id="2">
<totalPoints>450</totalPoints>
<amountEnrolled>2</amountEnrolled>
</student>
</course>
</root>
这可能不是你想要的,但它可以做你应该做的事情。
抱歉,我忘记在
内指定多少次<course>
好的,那么我们需要将计数移动到父课程元素:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:for-each select="all/courses/allcourses/course">
<course code="{c_code}">
<xsl:for-each select="student">
<student id="{studentID}">
<totalPoints><xsl:value-of select="sum(results/u_points)" /></totalPoints>
</student>
</xsl:for-each>
<amountEnrolled>
<xsl:value-of select="count(student[sum(results/u_points)!=360])"/>
</amountEnrolled>
</course>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
如果我们现在将输入修改为:
<all>
<courses>
<allcourses>
<course>
<c_code>U65</c_code>
<student>
<studentID>1</studentID>
<results>
<u_points>10</u_points>
</results>
<results>
<u_points>20</u_points>
</results>
<results>
<u_points>30</u_points>
</results>
</student>
<student>
<studentID>2</studentID>
<results>
<u_points>40</u_points>
</results>
<results>
<u_points>50</u_points>
</results>
<results>
<u_points>270</u_points>
</results>
</student>
</course>
</allcourses>
</courses>
</all>
我们将收到:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<course code="U65">
<student id="1">
<totalPoints>60</totalPoints>
</student>
<student id="2">
<totalPoints>360</totalPoints>
</student>
<amountEnrolled>1</amountEnrolled>
</course>
</root>