我想为输入创建一个转换以获得输出。我正在尝试各种转换,但没有用。我试着把 <xsl:for-each>
在 <fo:table-body>
之间和 <fo:table-row>
元素,但我得到一个子元素丢失的错误(它是 <fo:table-row>
)。任何帮助将不胜感激。
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</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>
最佳答案
从示例中找出如何针对每个错误提出建议有点困难,因为我认为它与 xpath 相关并且没有引用。
这里有一个替代解决方案,使用应用模板代替。 (而且我认为在这种情况下最好使用 apply-templates 然后 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>
希望这对您有所帮助。
附言如果您需要有关 for-each 循环的帮助,您能否发布您尝试过但没有成功的代码示例。
关于XSLT - 将表结构转换为 FO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7272800/