2013-07-23 86 views
0

我需要顯示兩個表:XSL:如何避免表之間的合併?

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


|  Col1 | Col2 | 
-------+------+--------- 
| c1.1 | c1.2 | c2 | 
-------+------+--------- 
| x11 | x21 | y01 | 
| x12 | x22 | y02 | 

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" >a21</stats> 
    </row> 
    <row name="2"> 
    <stats name="p1.1" >a12</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> 
</tb> 
<tb> 
<col title="Col1"> 
    <row name="1"> 
    <stats name="c1.1" >x11</stats> 
    <stats name="c1.2" >x21</stats> 
    </row> 
    <row name="2"> 
    <stats name="c1.1" >x12</stats> 
    <stats name="c1.2" >x22</stats> 
    </row> 
</col> 
<col title="Col2"> 
    <row name="1"> 
    <stats name="c2" >y01</stats> 
    </row> 
    <row name="2"> 
    <stats name="c2" >y02</stats> 
    </row> 
</col> 
</tb> 

XSL:

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

但我有兩個表之間的合併,就像這樣:

 
| Param1 | Param2 | Col1  | Col2 | 
-------+------+--------+------+------+-------- 
| p1.1 | p1.2 | p2 | c1.1 | c1.2 | c2 | 
-------+------+--------+------+------+-------- 
| a11 | a21 | b01 | x11 | x21 | y01 | 
| a12 | a22 | b02 | x12 | x22 | y02 | 

如何避免表之間的合併?

回答

1

您輸入的XML無效。它應該有一個根元素。假如你包裹你的XML中稱爲元素「表」:

<tables> 
    <tb> 
    ... 
    </tb> 
    <tb> 
    ... 
    </tb> 
</tables> 

你可以多一個模板添加到您當前的XSLT和更改當前XSLT第1模板的match屬性的值:

<xsl:template match="/*"> 
    <div> 
     <xsl:apply-templates select="tb" /> 
    </div> 
    </xsl:template> 

    <xsl:template match="tb"> <!-- Slash before tb removed --> 
    <table class="data_table" style="width: 100%; background:gray"> 
     ... 
    </table> 
    </xsl:template> 

當此經修改的樣本輸入運行,其結果是:

<div> 
    <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>a21</td> 
     <td>b01</td> 
     </tr> 
     <tr> 
     <td>a12</td> 
     <td>a22</td> 
     <td>b02</td> 
     </tr> 
    </tbody> 
    </table> 
    <table class="data_table" style="width: 100%; background:gray"> 
    <thead> 
     <tr> 
     <th colspan="2">Col1</th> 
     <th>Col2</th> 
     </tr> 
     <tr> 
     <th>c1.1</th> 
     <th>c1.2</th> 
     <th>c2</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <td>x11</td> 
     <td>x21</td> 
     <td>y01</td> 
     </tr> 
     <tr> 
     <td>x12</td> 
     <td>x22</td> 
     <td>y02</td> 
     </tr> 
    </tbody> 
    </table> 
</div>