Tecnologías XML / XSL-FO / Ejemplos de aplicación / Tablas complejas

 

Tablas complejas

Se genera una tabla con celdas que abarcan varias filas y columnas.

La estructura básica del capítulo anterior se amplía con celdas que abarcan filas y columnas. El principio para leer características tipográficas de la instancia XML se aplica a muchas propiedades de tablas y puede adaptarse a requisitos específicos.

El ejemplo parte de una tabla HTML cuyo cuerpo tiene cuatro filas. La primera tiene dos celdas. En la segunda, una celda abarca ambas columnas. En la tercera y cuarta, la primera de dos celdas abarca ambas filas.

Extracto de instancia XML:

<table>
   <tbody>
      <tr>
         <td>Zelle 1</td>
         <td>Zelle 2</td>
      </tr>
      <tr>
         <td colspan="2">Zelle 3</td>
      </tr>
      <tr>
         <td rowspan="2">Zelle 4</td>
         <td>Zelle 5</td></tr>
      <tr>
         <td>Zelle 6</td></tr>
   </tbody>
</table>

Hoja de estilo:

<xsl:template match="table"> 
   <fo:table>
      <xsl:apply-templates/>
   </fo:table>
</xsl:template>
<xsl:template match="tbody">
   <fo:table-body>
      <xsl:apply-templates/> 
   </fo:table-body>
</xsl:template>
<xsl:template match="tr">
   <fo:table-row>
      <xsl:apply-templates/>
   </fo:table-row>
</xsl:template>
<xsl:template match="td">                                        ❶  
   <fo:table-cell border-style="solid" border-width="0.5mm">     ❶
      <xsl:attribute name="number-columns-spanned">              ❷
         <xsl:value-of select="@colspan"/>                       ❷
      </xsl:attribute>
      <xsl:attribute name="number-rows-spanned">                 ❸
         <xsl:value-of select="@rowspan"/>                       ❸
      </xsl:attribute>
      <fo:block>
         <xsl:apply-templates/>
      </fo:block>
   </fo:table-cell>
</xsl:template>

❶ La plantilla crea las celdas. El elemento <fo:table-cell> define sus bordes con los atributos border-style y border-width.

❷ El atributo number-columns-spanned combina columnas; su valor indica cuántas. El elemento <xsl:attribute> crea un atributo en el elemento <fo:table-cell> cuyo nombre viene dado por el valor del atributo name, aquí number-columns-spanned. Su valor se lee de la instancia con <xsl:value-of>.

❸ El atributo number-rows-spanned combina filas siguiendo el mismo procedimiento que number-columns-spanned.

 

<< atrás

siguiente >>

volver arriba