2009-12-21 9 views
2

Si je le tableau suivant:Comment diviser une table verticalement en deux tables dans XSL?

<table> 
    <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr> 
    <tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr> 
</table> 

Comment pourrais-je partager ce XSLT pour que je me retrouve avec les éléments suivants:

<table> 
    <tr><td>1</td><td>2</td><td>3</td></tr> 
    <tr><td>A</td><td>B</td><td>C</td></tr> 
</table>  
<table> 
    <tr><td>4</td><td>5</td><td>6</td></tr> 
    <tr><td>D</td><td>E</td><td>F</td></tr> 
</table> 

Je suis intéressé par une méthode généralisée, où la table pourrait avoir des dimensions et être divisé en plus de deux tables. Je ne me soucie pas des rangées; Je veux diviser où il y a plus de N colonnes et finir avec des tables TD/N où TD est une cellule de données de table. Par exemple, s'il y a 12 colonnes et 25 lignes, j'aimerais 4 tables, chacune avec 3 colonnes et 25 lignes.

Répondre

1

Essayez ceci. Cela devrait fonctionner dans XSLT 1.0. Ajustez la variable ITEMS pour faire varier le nombre de colonnes que vous souhaitez pour chaque table.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="html" omit-xml-declaration="yes"/> 

    <xsl:variable name="ITEMS">3</xsl:variable> 

    <xsl:template match="//table"> 
     <!-- Loop through the items in the first row --> 
     <xsl:for-each select="tr[position() = 1]/td"> 
     <!-- Check if this item needs to be the start of a new row in a new table --> 
     <xsl:if test="position() mod $ITEMS = 1"> 
      <!-- Get the current position which is used to get items from subsequent rows --> 
      <xsl:variable name="COLUMNNUMBER" select="position()"/> 
      <table> 
       <!-- Loop through all the rows in the table --> 
       <xsl:for-each select="../../tr"> 
        <tr> 
        <!-- Output items within the required range using previously saved column number --> 
        <xsl:for-each select="td[position() &gt;= $COLUMNNUMBER and position() &lt; $COLUMNNUMBER + $ITEMS]"> 
         <xsl:copy-of select="."/> 
        </xsl:for-each> 
        </tr> 
       </xsl:for-each> 
      </table> 
     </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 
0

Voici mon opinion à ce:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> 
    <xsl:variable name="split" select="3" /> 

    <xsl:template match="table"> 
    <xsl:variable name="self" select="." /> 
    <!-- select <td>1</td>, <td>4</td> --> 
    <xsl:for-each select="tr[1]/td[position() mod $split = 1]"> 
     <xsl:apply-templates select="$self" mode="split"> 
     <!-- calculate & pass the starting position for copying <td>s --> 
     <xsl:with-param name="start" select="$split * (position() - 1)" /> 
     </xsl:apply-templates> 
    </xsl:for-each> 
    </xsl:template> 

    <!-- this just copies the table an passes on $start --> 
    <xsl:template match="table" mode="split"> 
    <xsl:param name="start" select="0" /> 
    <xsl:copy> 
     <xsl:apply-templates select="tr" mode="split"> 
     <xsl:with-param name="start" select="$start" /> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

    <!-- this copies <tr>/<td> based on $start --> 
    <xsl:template match="tr" mode="split"> 
    <xsl:param name="start" select="0" /> 
    <xsl:copy> 
     <xsl:copy-of select="td[ 
     position() &gt; $start and position() &lt;= $start + $split 
     ]" /> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

Résultat:

<table> 
    <tr> 
    <td>1</td><td>2</td><td>3</td> 
    </tr> 
    <tr> 
    <td>A</td><td>B</td><td>C</td> 
    </tr> 
</table> 
<table> 
    <tr> 
    <td>4</td><td>5</td><td>6</td> 
    </tr> 
    <tr> 
    <td>D</td><td>E</td><td>F</td> 
    </tr> 
</table> 
Questions connexes