![](/img/trans.png)
[英]Transform an HTML DL into 2-column XSL-FO table of terms and definitions
[英]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() < $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() < $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
主要是為了避免答案中的縮進過多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.