我正在编写一个XSLT脚本,它生成如下所示的HTML输出:
Alice 0
Bob Maths 74
Science 84
Mary Maths 80
Science 90
History 95
Mark Maths 70
Tim 0
我想为每个学生添加一个总单元格,表格应该像一样
Alice Total 0
Bob Maths 74
Science 84
Total 158
Mary Maths 80
Science 90
History 95
Total 265
Mark Maths 70
Total 70
Tim Total 0
<currentRow>
<columnValue>alice</columnValue>
<columnValue><null/></columnValue>
<columnValue>0</columnValue>
</currentRow>
<currentRow>
<columnValue>bob</columnValue>
<columnValue>maths</columnValue>
<columnValue>74</columnValue>
</currentRow>
<currentRow>
<columnValue>bob</columnValue>
<columnValue>science</columnValue>
<columnValue>84</columnValue>
</currentRow>
<currentRow>
<columnValue>mary</columnValue>
<columnValue>maths</columnValue>
<columnValue>80</columnValue>
</currentRow>
<currentRow>
<columnValue>mary</columnValue>
<columnValue>science</columnValue>
<columnValue>90</columnValue>
</currentRow>
<currentRow>
<columnValue>mary</columnValue>
<columnValue>history</columnValue>
<columnValue>95</columnValue>
</currentRow>
<currentRow>
<columnValue>mark</columnValue>
<columnValue>maths</columnValue>
<columnValue>70</columnValue>
</currentRow>
<currentRow>
<columnValue>tim</columnValue>
<columnValue><null/></columnValue>
<columnValue>0</columnValue>
</currentRow>
这是我的xml结构
XSLT为每一行运行,如图所示:
<xsl:for-each select="//wrs:currentRow">
<xsl:variable name="theCurrentStudent">
<xsl:value-of select="wrs:columnValue[1]"/>
</xsl:variable>
<xsl:element name="tr">
<xsl:if test="not(preceding-sibling::wrs:currentRow/wrs:columnValue[1]=$theCurrentStudent)">
<xsl:element name="td">
<xsl:attribute name="rowspan">
<xsl:value-of select="count(//wrs:currentRow[wrs:columnValue[1]=$theCurrentStudent])"/>
</xsl:attribute>
<xsl:value-of select="$theCurrentStudent"/>
</xsl:element>
</xsl:if>
<xsl:element name="td">
<xsl:value-of select="wrs:columnValue[2]"/>
</xsl:element>
<xsl:element name="td">
<xsl:value-of select="wrs:columnValue[3]">
<xsl:element name="tr">
<xsl:value-of select="sum(wrs:columnValue[3])"/>
</xsl:element>
</xsl:value-of>
</xsl:element>
</xsl:element>
</xsl:for-each>
除此之外,所有东西都在脚本中工作,有什么想法可以在XSLT脚本中做到这一点吗?
此转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kSameStudent" match="currentRow"
use="generate-id(preceding-sibling::currentRow
[columnValue[1] = current()/columnValue[1]]
[last()]
)"/>
<xsl:template match="/*">
<html>
<table border="1">
<xsl:apply-templates select=
"*[not(columnValue[1]
=
preceding-sibling::*[1]/columnValue[1]
)]"/>
</table>
</html>
</xsl:template>
<xsl:template match="currentRow">
<xsl:variable name="vGroup" select=
". | key('kSameStudent', generate-id())"/>
<xsl:apply-templates mode="inGroup" select="$vGroup" />
<tr>
<td> </td>
<td>Total</td>
<td><xsl:value-of select=
"sum($vGroup/columnValue[3])"/>
</td>
</tr>
</xsl:template>
<xsl:template match="currentRow" mode="inGroup">
<xsl:variable name="vPos" select="position()"/>
<tr>
<td>
<xsl:value-of select=
"concat(' ', columnValue[1][$vPos = 1])"/>
</td>
<td>
<xsl:value-of select=
"concat(' ', columnValue[2][not(null)])"/>
</td>
<td>
<xsl:value-of select=
"concat(' ', columnValue[3])"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
应用于所提供的XML文档时(格式良好):
<t>
<currentRow>
<columnValue>alice</columnValue>
<columnValue>
<null/>
</columnValue>
<columnValue>0</columnValue>
</currentRow>
<currentRow>
<columnValue>bob</columnValue>
<columnValue>maths</columnValue>
<columnValue>74</columnValue>
</currentRow>
<currentRow>
<columnValue>bob</columnValue>
<columnValue>science</columnValue>
<columnValue>84</columnValue>
</currentRow>
<currentRow>
<columnValue>mary</columnValue>
<columnValue>maths</columnValue>
<columnValue>80</columnValue>
</currentRow>
<currentRow>
<columnValue>mary</columnValue>
<columnValue>science</columnValue>
<columnValue>90</columnValue>
</currentRow>
<currentRow>
<columnValue>mary</columnValue>
<columnValue>history</columnValue>
<columnValue>95</columnValue>
</currentRow>
<currentRow>
<columnValue>mark</columnValue>
<columnValue>maths</columnValue>
<columnValue>70</columnValue>
</currentRow>
<currentRow>
<columnValue>tim</columnValue>
<columnValue>
<null/>
</columnValue>
<columnValue>0</columnValue>
</currentRow>
</t>
生成所需的正确结果:
<html>
<table border="1">
<tr>
<td> alice</td>
<td> </td>
<td> 0</td>
</tr>
<tr>
<td> </td>
<td>Total</td>
<td>0</td>
</tr>
<tr>
<td> bob</td>
<td> maths</td>
<td> 74</td>
</tr>
<tr>
<td> </td>
<td> science</td>
<td> 84</td>
</tr>
<tr>
<td> </td>
<td>Total</td>
<td>158</td>
</tr>
<tr>
<td> mary</td>
<td> maths</td>
<td> 80</td>
</tr>
<tr>
<td> </td>
<td> science</td>
<td> 90</td>
</tr>
<tr>
<td> </td>
<td> history</td>
<td> 95</td>
</tr>
<tr>
<td> </td>
<td>Total</td>
<td>265</td>
</tr>
<tr>
<td> mark</td>
<td> maths</td>
<td> 70</td>
</tr>
<tr>
<td> </td>
<td>Total</td>
<td>70</td>
</tr>
<tr>
<td> tim</td>
<td> </td>
<td> 0</td>
</tr>
<tr>
<td> </td>
<td>Total</td>
<td>0</td>
</tr>
</table>
</html>
在前臂内侧,您必须重新计算总分
<xsl:for-each select="student/subject">
<xsl:variable name="scoreBase" select="score" />
<xsl:variable name="calculatedTotal"
select="student/total/score" />
<xsl:variable name="calculatedTotal" select="$scoreBase + $calculatedTotal" />
</xsl:for-each>
你能在foreach 的末尾尝试一下吗
<xsl:variable name="temp" select="sum(preceding-sibling::wrs:columnValue[3])"/>
<td><xsl:value-of select="$temp"/></td>