0

我需要显示两个表:

| 参数1 | 参数2 |
--------+------+----------
| 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>

但是我在两个表之间进行了合并,如下所示:

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

如何避免表之间的合并?

4

1 回答 1

1

您的输入 XML 无效。它应该有一个根元素。假设您将 XML 包装在一个名为“tables”的元素中:

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

您可以向当前 XSLT 添加一个模板,并更改当前 XSLT 中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>
于 2013-07-23T18:20:35.367 回答