2013-07-23 112 views
2

對不起,我正在學習XSL,我想顯示像這樣的表:XSL:如何改進此代碼?

 
| Param1 | Param2 | 
-------+------+--------- 
| p1.1 | p1.2 | p2 | 
-------+------+--------- 
| a11 | a21 | b01 | 
| a12 | a22 | b02 | 

現在,我有一個xml:

<?xml version="1.0" encoding="UTF-8"?> 
<tb> 
    <col title="Param1"> 
     <row name="1"> 
     <stats name="p1.1" >a11</stats> 
     <stats name="p1.2" >a12</stats> 
     </row> 
     <row name="2"> 
     <stats name="p1.1" >a21</stats> 
     <stats name="p1.2" >a22</stats> 
     </row> 
    </col> 
    <col title="Param2"> 
     <row name="1"> 
     <stats name="p2" >b01</stats> 
     </row> 
     <row name="2"> 
     <stats name="p2" >b02</stats> 
     </row> 
    </col> 

和xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:for-each select="tb"> 
    <table class="data_table" style="width: 100%; background:gray"> 
     <thead> 
      <tr> 
     <xsl:for-each select="col"> 
      <xsl:choose> 
      <xsl:when test="count(row[1]/stats) > 1"> 
       <th colspan="{count(row[1]/stats)}"> 
        <xsl:value-of select="@title" /> 
       </th> 
      </xsl:when> 
      <xsl:otherwise> 
       <th><xsl:value-of select="@title" /></th> 
      </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each> 
    </tr> 
    <tr> 
     <xsl:for-each select="col/row[1]/stats"> 
      <th><xsl:value-of select="@name" /></th> 
     </xsl:for-each> 
    </tr> 
     </thead> 
     <tbody> 

      <tr> 
       <xsl:for-each select="col/row[1]/stats"> 
       <td> 
        <xsl:value-of select="." /> 
       </td> 
       </xsl:for-each> 
       </tr> 
       <tr> 
       <xsl:for-each select="col/row[2]/stats"> 
       <td> 
       <xsl:value-of select="." /> 
      </td> 
        </xsl:for-each> 
      </tr> 
     </tbody> 
     </table> 
    </xsl:for-each> 

這是行得通的,但我如何改進這個代碼使用單個FOR來組裝表格行S'在這個例子中,我只有2行(a11 | a21 | b01和a12 | a22 | b02)和3列,但這可能會改變(20行,4列...)。也許我需要將屬於同一行的單元分組。

回答

4

要改善XSLT的第一件事就是使用模板。一旦完成,您可以執行以下操作來處理任意數量的行。

此解決方案假定您的源數據的每<col>將有<row>爲每個需要行:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/tb"> 
    <table class="data_table" style="width: 100%; background:gray"> 
     <thead> 
     <tr> 
      <xsl:apply-templates select="col" mode="titles" /> 
     </tr> 
     <tr> 
      <xsl:apply-templates select="col/row[1]/stats" mode="titles" /> 
     </tr> 
     </thead> 
     <tbody> 
     <xsl:apply-templates select="col[1]/row" /> 
     </tbody> 
    </table> 

    </xsl:template> 

    <xsl:template match="col" mode="titles"> 
    <th> 
     <xsl:apply-templates select="(.)[row[1]/stats[2]]" mode="colSpan" /> 
     <xsl:value-of select="@title"/> 
    </th> 
    </xsl:template> 

    <xsl:template match="col" mode="colSpan"> 
    <xsl:attribute name="colspan"> 
     <xsl:value-of select="count(row[1]/stats)"/> 
    </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="stats" mode="titles"> 
    <th> 
     <xsl:value-of select="@name" /> 
    </th> 
    </xsl:template> 

    <xsl:template match="row"> 
    <tr> 
     <xsl:apply-templates select="../../col/row[@name = current()/@name]/stats" /> 
    </tr> 
    </xsl:template> 

    <xsl:template match="stats"> 
    <td> 
     <xsl:value-of select="." /> 
    </td> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行,其結果是:

<table class="data_table" style="width: 100%; background:gray"> 
    <thead> 
    <tr> 
     <th colspan="2">Param1</th> 
     <th>Param2</th> 
    </tr> 
    <tr> 
     <th>p1.1</th> 
     <th>p1.2</th> 
     <th>p2</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>a11</td> 
     <td>a12</td> 
     <td>b01</td> 
    </tr> 
    <tr> 
     <td>a21</td> 
     <td>a22</td> 
     <td>b02</td> 
    </tr> 
    </tbody> 
</table>