添加数值列值并在XSLT中插入总计

时间:2012-03-27 16:37:52

标签: html xml xslt-1.0

我正在编写一个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为每一行运行for-each,如下所示:

<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脚本中如何做到这一点?

3 个答案:

答案 0 :(得分:1)

此转化

<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>&#xA0;</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('&#xA0;', columnValue[1][$vPos = 1])"/>
   </td>
   <td>
    <xsl:value-of select=
    "concat('&#xA0;', columnValue[2][not(null)])"/>
   </td>
   <td>
    <xsl:value-of select=
    "concat('&#xA0;', 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>&nbsp;alice</td>
         <td>&nbsp;</td>
         <td>&nbsp;0</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>Total</td>
         <td>0</td>
      </tr>
      <tr>
         <td>&nbsp;bob</td>
         <td>&nbsp;maths</td>
         <td>&nbsp;74</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>&nbsp;science</td>
         <td>&nbsp;84</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>Total</td>
         <td>158</td>
      </tr>
      <tr>
         <td>&nbsp;mary</td>
         <td>&nbsp;maths</td>
         <td>&nbsp;80</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>&nbsp;science</td>
         <td>&nbsp;90</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>&nbsp;history</td>
         <td>&nbsp;95</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>Total</td>
         <td>265</td>
      </tr>
      <tr>
         <td>&nbsp;mark</td>
         <td>&nbsp;maths</td>
         <td>&nbsp;70</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>Total</td>
         <td>70</td>
      </tr>
      <tr>
         <td>&nbsp;tim</td>
         <td>&nbsp;</td>
         <td>&nbsp;0</td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>Total</td>
         <td>0</td>
      </tr>
   </table>
</html>

答案 1 :(得分:0)

在你的foreach中,你将不得不重新计算总分

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

答案 2 :(得分:0)

你可以在foreach结束时尝试这个吗

<xsl:variable name="temp" select="sum(preceding-sibling::wrs:columnValue[3])"/> 
<td><xsl:value-of select="$temp"/></td>