XSL-FO two column table



我需要创建一个2列的表,由不固定数量的图像填充,每个单元格一个图像,每行两个图像。我们使用XSL-FO

我有这个XSL数据作为XSL 的输入

<viewList>
    <views order="1">data:image/jpg;base64,/9j/4AAQSkZJRg...(base64 data)</views>
    <views order="2">data:image/jpg;base64,/9j/4AAQSkZ432...(base64 data)</views>
    <views order="3">data:image/jpg;base64,/9j/4AAQSkZdgd...(base64 data)</views>
    <views order="4">data:image/jpg;base64,/9j/4AAQSkZ775...(base64 data)</views>
    <views order="5">data:image/jpg;base64,/9j/4AAQSk7655...(base64 data)</views>
</viewList>

正如您所注意到的,标签views的内容是base64信息,编码图像。我刚刚截断了它们;它们相当长。

对于渲染图像,我使用标记fo:external-graphic。因此,我设法在一张表中绘制所有图像:

<fo:table>
<fo:table-body>   
    <xsl:for-each select="viewList/views">                                      
        <fo:table-row>
            <fo:table-cell>                                        
                <fo:block text-align="center">
                    <fo:external-graphic src="{current()}"/>
                </fo:block>                    
            </fo:table-cell>
        </fo:table-row>
    </xsl:for-each>
</fo:table-body>  
</fo:table>

正如你所看到的;这是无效的;因为它是每个细胞的图像。。。你知道如何把它们分成两列吗?

实现这一点的一种方法是最初只选择将成为每行中第一个单元格的views元素。

 <xsl:for-each select="viewList/views[position() mod $cellsPerRow = 1]">

(其中cellsPerRow是一个设置为2的变量,这样可以轻松更改每行的单元格数)。这将构成您的fo:table-row)的基础

然后,要选择实际形成行的单元格,您可以执行以下

<xsl:for-each select="self::*|following-sibling::views[position() &lt; $cellsPerRow]" >

尝试此XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="fo" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:param name="cellsPerRow" select="2" />
    <xsl:template match="/">
        <fo:table>
            <fo:table-body>   
                <xsl:for-each select="viewList/views[position() mod $cellsPerRow = 1]">                                      
                    <fo:table-row>
                        <xsl:apply-templates select="self::*|following-sibling::views[position() &lt; $cellsPerRow]" />
                    </fo:table-row>
                </xsl:for-each>
            </fo:table-body>  
        </fo:table>
    </xsl:template>
    <xsl:template match="views">
        <fo:table-cell>                                        
            <fo:block text-align="center">
                <fo:external-graphic src="{current()}"/>
            </fo:block>                    
        </fo:table-cell>
    </xsl:template>
</xsl:stylesheet>

注意,我已经从使用xsl:for-each切换到使用xsl:apply-templates,主要是为了避免答案中出现过多的缩进。

最新更新