2016-10-03 94 views
1

也許你可以幫助我。 我不知道如何總結一些表格的單元格「Year」,它們是按「Title」分組的。我需要這個總和單元格也會合併爲第一個單元格「標題」。XSLT總分組表格單元格

我用總和(),但它returs 0

XML代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<LIST> 
<Row> 
    <TITLE>Empire Burlesque</TITLE> 
    <YEAR>2001</YEAR> 
    <artist>Bob Dylan</artist> 
    <artist1>Bob Dylan1</artist1> 
</Row> 
<Row> 
    <TITLE>Empire Burlesque</TITLE> 
    <YEAR>2002</YEAR> 
    <artist>Bob Dylan</artist> 
    <artist1>Bob Dylanas</artist1> 
</Row> 
<Row> 
    <TITLE>Empire Burlesque</TITLE> 
    <YEAR>2003</YEAR> 
    <artist>Bonnie Tyler</artist> 
    <artist1>Bob Dylan</artist1> 
</Row> 
<Row> 
    <TITLE>Empire Burlesque</TITLE> 
    <YEAR>2004</YEAR> 
    <artist>Bonnie Tyler</artist> 
    <artist1>Bob Dylanas</artist1> 
</Row> 
<Row> 
    <TITLE>Empire Burlesque1</TITLE> 
    <YEAR>2005</YEAR> 
    <artist>Bonnie Tyler</artist> 
    <artist1>Bob Dylan</artist1> 
</Row> 
<Row> 
    <TITLE>Empire Burlesque1</TITLE> 
    <YEAR>2006</YEAR> 
    <artist>Bonnie Tyler</artist> 
    <artist1>Bob Dylanas</artist1> 
</Row> 
</LIST> 

XSLT代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:key name="cds" match="Row" use="TITLE" /> 

    <xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Year</th> 
     <th>Artist</th> 
     <th>Artist1</th> 
    </tr> 
<xsl:for-each select="LIST/Row[generate-id() = generate-id(key('cds', TITLE)[1])]" > 
    <tr> 
    <td> 
     <xsl:if test="key('cds', TITLE)[1]"> 
      <xsl:attribute name="rowspan"> 
       <xsl:value-of select="count(key('cds', TITLE))" /> 
      </xsl:attribute> 
     </xsl:if> 
     <xsl:value-of select="TITLE"/> 
    </td>  
    <td> 
    <xsl:value-of select="YEAR"/> 
    </td> 
    <td> 
     <xsl:value-of select="artist"/> 
    </td> 
    <td>  
     <xsl:value-of select="artist1"/> 
    </td> 
</tr>  
<xsl:for-each select="key('cds', TITLE)[position() > 1]"> 
<tr> 
    <td> 
    <xsl:value-of select="YEAR"/> 
    </td> 
    <td> 
     <xsl:value-of select="artist"/> 
    </td> 
    <td> 
     <xsl:value-of select="artist1"/> 
    </td> 
</tr>  
</xsl:for-each> 
    </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

現在的結果是:

enter image description here

+0

你可以顯示你已經嘗試過的sum(),即使它只返回0也可以顯示你在這種情況下期望的輸出(最好是代碼而不是圖像)?謝謝! –

回答

1

嘗試sum(key('cds', TITLE)/YEAR)內部for-each。