xslt创建一个计数器变量并打印结束值



下面的xslt是从sqldb获取数据的模板。(我使用javaspring体系结构将值传递给xslt(。可以从数据库中提取n个值(最大500(。studentid和subject是从DB中提取的两个值。以下是数据示例

Student id | Subject
3111 | 101
3112 | 100
3113 | 110
3114 | 001

学科字段包含一个3位数的值,表示学生所学的物理、化学和数学科目。110表示学生只参加物理和化学课程。我需要计算三门科目的总和,比如分别有多少学生选修了物理、化学和数学。

我先开始尝试物理,但还没有成功。下面是我使用position((创建的xslt来计算sum。但物理学的总和输出显示如下(假设有6名学生注册了物理学(物理计数:123456

xslt

<?xml version="1.0" encoding="UTF-8"/>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="newLine" select="'&#10;'"/>
<xsl:text>
Student IDs:
</xsl:text>
<xsl:apply-templates select="//studentid"/>
<xsl:text>
Physics students: 
</xsl:text>
<xsl:apply-templates select="//subjects"/>
<xsl:template match="studentid>
<xsl:value-of select="concat(.,$newLine)"/>
</xsl:template>
<xsl:template match="subjects">
<xsl:choose>
<xsl:when test=".='100'">
<xsl:value-of select="position()"/>
</xsl:when>
<xsl:when test=".='110'">
<xsl:value-of select="position()"/>
</xsl:when>
<xsl:when test=".='111'">
<xsl:value-of select="position()"/>
</xsl:when>
<xsl:when test=".='101'">
<xsl:value-of select="position()"/>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

学生样本输入:

110
110
101
111
100

电流输出:

Physics students: 12345

预期输出:

Physics students: 5

(我将把物理学生的解决方案应用于化学和数学科目(。提前感谢!

这听起来更像是一个分组问题,可以计算每个组的成员:

<xsl:for-each-group select="student" group-by="subject">
<xsl:value-of select="current-grouping-key(), count(current-group())" separator=": "/>
</xsl:for-each-group>

您还没有显示XML输入结构,所以select="student"group-by="subject"只是猜测,您可能需要对它们进行调整。

最新更新