问题
I'm getting crazy with spanning cells of a table. My table is of 3 columns. Below you see one row (only fragment):
<tgroup>
<colspec name="x">
<colspec name="y">
<colspec name="z">
<tbody>
<row>
<entry>abc
<entry namest="y" nameend="z">blabla
The second entry (blabla) should span two entry (=table cells). The information is in the attributes "nameend" and "namest".
My way is:
xsl:template match="entry" ...
get the position number of
<colspec name=z>
(here 3) and of<colspec name="y">
(here 2)<??????>
substract z and y (=1) add 1: result=3
<xsl:param name="colspan"> <xsl:value-of select="($nameend)-($namest)+(1)"/> </xsl:param>
use the result=3 as attribute "colspan" in the entry template
<fo:table-cell number-columns-spanned="{$colspan}"
But I see no way to solve my second step (????)
Any ideas?? Thanks Pia
P.S. No, I can not change the source file
回答1:
Given this input XML:
<?xml version="1.0" encoding="UTF-8"?>
<tgroup>
<colspec name="x"/>
<colspec name="y"/>
<colspec name="z"/>
<tbody>
<row>
<entry>abc</entry>
<entry namest="y" nameend="z">blabla 1</entry>
</row>
<row>
<entry namest="x" nameend="z">blabla 2</entry>
</row>
<row>
<entry namest="x" nameend="y">blabla 3</entry>
<entry>cde</entry>
</row>
</tbody>
</tgroup>
This XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="entry[@namest and @nameend]">
<xsl:variable name="namest" select="@namest"/>
<xsl:variable name="nameend" select="@nameend"/>
<xsl:variable name="namestPos" select="count(ancestor::tgroup/colspec[@name=$namest]/preceding-sibling::colspec)"/>
<xsl:variable name="nameendPos" select="count(ancestor::tgroup/colspec[@name=$nameend]/preceding-sibling::colspec)"/>
<table-cell number-columns-spanned="{$nameendPos - $namestPos + 1}">
<xsl:apply-templates/>
</table-cell>
</xsl:template>
</xsl:stylesheet>
Will yield this output:
<?xml version="1.0" encoding="UTF-8"?>
<tgroup>
<colspec name="x"/>
<colspec name="y"/>
<colspec name="z"/>
<tbody>
<row>
<entry>abc</entry>
<table-cell number-columns-spanned="2">blabla 1</table-cell>
</row>
<row>
<table-cell number-columns-spanned="3">blabla 2</table-cell>
</row>
<row>
<table-cell number-columns-spanned="2">blabla 3</table-cell>
<entry>cde</entry>
</row>
</tbody>
</tgroup>
Notes:
- Although you wrote, "subtract z and y (=1) add 1: result=3", I assume that you meant "result=2".
entry
is mapped totable-cell
with the desired@number-columns-spanned
attribute value, which is the key aspect of the question. Remapping of the surrounding elements and mapping to thefo
namespace too remain to do.
来源:https://stackoverflow.com/questions/20408261/xsl-cals-tables-span-cells-using-colspec-namest-and-nameend