如何使用XSL-FO / Apache FOP在表行中重复相同的模板

我正在尝试从我已经获得的XML文件创建一个PDF,到目前为止它使用XSL-FO / Apache FOP工作得相当好。

XML文件基本上包含条形码信息:条形码本身和条形码类型(我也会在某些时候添加条形码图像)。

现在我想看到的输出是这样的:

 -----------------------
| barcode1  | barcode2  |
| codetype1 | codetype2 |
 -----------------------
| barcode3  | barcode4  |
| codetype3 | codetype4 |
 -----------------------

等等。

我已经定义了以下xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
  <xsl:template match="barcode-list">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simpleA4">
        <fo:flow flow-name="xsl-region-body">
          <fo:block font-size="10pt">
          <fo:table table-layout="fixed" width="100%" border-collapse="separate">    
            <fo:table-column column-width="45%"/>
            <fo:table-column column-width="45%"/>
            <fo:table-body>
              <xsl:apply-templates select="item"/>
            </fo:table-body>
          </fo:table>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
     </fo:root>
  </xsl:template>
  <xsl:template match="item">
    <fo:table-row>   
      <fo:table-cell>
        <fo:block wrap-option="wrap">
          <xsl:value-of select="name"/>
        </fo:block>
        <fo:block wrap-option="wrap">
          <xsl:value-of select="format"/>
        </fo:block>
      </fo:table-cell> 
    </fo:table-row>
  </xsl:template>
</xsl:stylesheet>

所以有两列,我假设我可以将它指向“项目”模板以填充单元格。

现在我意识到“item”模板包含一个<table-row>标签,这会导致每个项目出现在自己的表格行中。 所以我得到的是:

 -----------------------
| barcode1  |           |
| codetype1 |           |
 -----------------------
| barcode2  |           |
| codetype2 |           |
 -----------------------
| barcode3  |           |
| codetype3 |           |
 -----------------------
| barcode4  |           |
| codetype4 |           |
 -----------------------

我的问题是如何更改xsl以获得所需的输出而不是将每个项目放在自己的表行中?

author: baz
采纳答案:

省略fo:table-row并使用很少使用的starts-rowhttps://www.w3.org/TR/xsl11/#starts-row )和/或ends-rowhttps://www.w3) .org / TR / xsl11 /#ends-row )属性:

<xsl:template match="item">
  <fo:table-cell>
    <xsl:if test="position() mod 2 = 1">
      <xsl:attribute name="starts-row">true</xsl:attribute>
    </xsl:if>
    <fo:block wrap-option="wrap">
      <xsl:value-of select="name"/>
    </fo:block>
    <fo:block wrap-option="wrap">
      <xsl:value-of select="format"/>
    </fo:block>
  </fo:table-cell> 
</xsl:template>
author: tony-graham

参考更多解答: How to repeat same template in table-row using XSL-FO/Apache FOP ,转载请保留出处如何使用XSL-FO / Apache FOP在表行中重复相同的模板及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:xml