简体   繁体   English

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

[英]How to repeat same template in table-row using XSL-FO/Apache FOP

I'm trying to create a PDF from an XML file I've got and it's working fairly well so far using XSL-FO/Apache FOP. 我正在尝试从已有的XML文件创建PDF,到目前为止,使用XSL-FO / Apache FOP的效果都很好。

The XML file basically contains barcode information: the barcode itself and the barcode type (I'll add the barcode image at some point as well). XML文件基本上包含条形码信息:条形码本身和条形码类型(我还将在某些时候添加条形码图像)。

Now what I'd like to see as the output is this: 现在,我希望看到的输出是这样的:

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

And so on. 等等。

I've defined the following xsl: 我定义了以下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>

So there are two columns and I assumed I could just point it at the "item" template to fill in the cells. 所以有两列,我假设我可以将其指向“ item”模板来填写单元格。

Now I realize that the "item" template contains a <table-row> tag and that that causes each item to appear in its own table row. 现在,我意识到“ item”模板包含一个<table-row>标记,该标记使每个项目都出现在其自己的表行中。 So what I get is this: 所以我得到的是:

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

My question is how to change the xsl to get the desired output rather than getting each item in its own table row? 我的问题是如何更改xsl以获取所需的输出,而不是使每个项目都位于其自己的表行中?

Omit the fo:table-row and use the rarely-used starts-row ( https://www.w3.org/TR/xsl11/#starts-row ) and/or ends-row ( https://www.w3.org/TR/xsl11/#ends-row ) properties: 省略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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM