Reputation: 27
I needed to display two tables:
| 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>
But I have a merge between two tables, like this:
| 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 |
How to avoid merge between tables?
Upvotes: 0
Views: 56
Reputation: 101748
Your input XML is not valid. It should have a single root element. Supposing you wrapped your XML in an element called "tables":
<tables>
<tb>
...
</tb>
<tb>
...
</tb>
</tables>
You could add one more template to your current XSLT, and change the value of the match
attribute on the first template in your current XSLT:
<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>
When this is run on the modified sample input, the result is:
<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>
Upvotes: 1