Dynamic html table creation with single column rows
What I need to do is create a table with a variable name of columns (call it x). The issue I'm having is that any of the fields that could turn into a td can be a "single row item" aka have a colspan equal to x. The problem is that getting the row to start and end in a way which is consistent with the well formatting rules required by XML has been hard.
Here is what the data would look like:
<status>
<section title="">
<columns>3</columns>
<fields>
<field label="Label 1">test data 1</field>
<field label="Label 2">test data 2</field>
<field label="Label 3">test data 3</field>
<field singleRow="true" label="Label 4">test data 4</field>
<field label="Label 5">test data 5</field>
<field label="Label 6">test data 6</field>
<field label="Label 7">test data 7</field>
<field label="Label 8">test data 8</field>
<field label="Label 9">test data 9</field>
<field singleRow="true" label="Label 10">test data 10</field>
<field label="Label 11">test data 11</field>
<field label="Label 12">test data 12</field>
<field label="Label 13">test data 13</field>
<field label="Label 14">test data 14</field>
</fields>
</section>
The output should be as following:
<table>
<tr>
<td>Label 1</td>
<td>test data 1</td>
<td>Label 2</td>
<td>test data 2</td>
<td>Label 3</td>
<td>test data 3</td>
</tr>
<tr>
<td>Label 4</td>
<td colspan='5'>test data 4</td>
</tr>
<tr>
<td>Label 5</td>
<td>test data开发者_开发技巧 5</td>
<td>Label 6</td>
<td>test data 6</td>
<td>Label 7</td>
<td>test data 7</td>
</tr>
<tr>
<td>Label 8</td>
<td>test data 8</td>
<td>Label 9</td>
<td colspan='3'>test data 9</td>
</tr>
<tr>
<td>Label 10</td>
<td colspan='5'>test data 10</td>
</tr>
<tr>
<td>Label 11</td>
<td>test data 11</td>
<td>Label 12</td>
<td>test data 12</td>
<td>Label 13</td>
<td>test data 13</td>
</tr>
<tr>
<td>Label 14</td>
<td colspan='5'>test data 14</td>
</tr>
</table>
Is there a way to group elements in such a way to be able to do a for-each group for outputting each row? I mucked around with group-by but I haven't found a good way of doing this. The rule would be to separate into groups all nodes from the current point until the count = x (in the example above that is 3) or until an element has the singleRow attribute.
What would you think an elegant solution is to this problem?
Hmm, misread your requirements a bit, this almost does what you want... I need to go out now though, hope it helps somewhat, will try and come back later and fix it :)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="field">
<td><xsl:value-of select="@label" /></td>
<td><xsl:value-of select="text()" /></td>
</xsl:template>
<xsl:template match="fields">
<tbody>
<xsl:for-each-group group-adjacent="exists(@singleRow)" select="*">
<xsl:choose>
<xsl:when test="current-grouping-key() = true()">
<tr>
<xsl:apply-templates select="current-group()" />
</tr>
</xsl:when>
<xsl:otherwise>
<xsl:for-each-group select="current-group()" group-adjacent="position() lt 4">
<tr>
<xsl:apply-templates select="current-group()" />
</tr>
</xsl:for-each-group>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</tbody>
</xsl:template>
<xsl:template match="section">
<xsl:variable name="max" select="number(columns) + 1" />
<xsl:variable name="colspan" select="columns" />
<table>
<caption><xsl:value-of select="@title" /></caption>
<tbody>
<xsl:for-each-group group-adjacent="exists(@singleRow)" select="fields/*">
<xsl:choose>
<xsl:when test="current-grouping-key() = true()">
<tr colspan="{$colspan}">
<xsl:apply-templates select="current-group()" />
</tr>
</xsl:when>
<xsl:otherwise>
<xsl:for-each-group select="current-group()" group-adjacent="position() lt $max">
<tr>
<xsl:apply-templates select="current-group()" />
</tr>
</xsl:for-each-group>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
精彩评论