XSLT - transform a table structure to FO
I would like to create a transformation for the INPUT to get the OUTPUT. I was trying various transformations but it didn't work. I tried to put <xsl:for-each>
between <fo:table-body>
and <fo:table-row>
elements but I got an error that a child element is missing (it was the <fo:table-row>
). Any help would be appreciated.
INPUT
<table>
<tgroup>
<thead>
<row>
<entry>A</entry>
<entry>B</entry>
</row>
</thead>
<tbody>
<row>
<entry>a1</entry>
<entry>a2</entry>
</row>
<row>
<entry>b1</entry>
<entry>b2</entry>
</row>
</tbody>
</tgroup>
</table>
OUTPUT
<fo:table>
<fo:table-body>
<fo:table-row font-weight="bold">
<fo:table-cell>
<fo:block>A</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>B</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>a1&开发者_开发知识库lt;/fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>a2</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>b1</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>b2</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
It's a little hard from a sample to figure out how to advise on for-each error, because I think it's xpath related and there is no reference to it.
Here's an alternative solution, using apply-templates instead. (and also I think in this case it's better to use apply-templates then for-each):
<xsl:template match="table">
<fo:table>
<fo:table-body>
<xsl:apply-templates select="tgroup/*/row"/>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="thead/row">
<fo:table-row font-weight="bold">
<xsl:apply-templates select="entry" />
</fo:table-row>
</xsl:template>
<xsl:template match="tbody/row">
<fo:table-row>
<xsl:apply-templates select="entry" />
</fo:table-row>
</xsl:template>
<xsl:template match="entry">
<fo:table-cell>
<fo:block><xsl:value-of select="."/></fo:block>
</fo:table-cell>
</xsl:template>
Hope this helps.
P.S. If you need a help with for-each loop, can you post a sample of a code that you tried and it didn't work.
精彩评论