如何使用 XSL-FO/Apache FOP 在表行中重复相同的模板 [英] How to repeat same template in table-row using XSL-FO/Apache FOP

查看:25
本文介绍了如何使用 XSL-FO/Apache FOP 在表行中重复相同的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我获得的 XML 文件创建 PDF,到目前为止它使用 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 文件基本上包含条码信息:条码本身和条码类型(我也会在某个时候添加条码图像).

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).

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

Now what I'd like to see as the output is this:

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

等等.

我定义了以下 xsl:

I've defined the following 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"模板包含一个 <table-row> 标签,这会导致每个项目出现在它自己的表格行中.所以我得到的是:

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. So what I get is this:

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

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

My question is how to change the xsl to get the desired output rather than getting each item in its own table row?

推荐答案

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

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:

<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>

这篇关于如何使用 XSL-FO/Apache FOP 在表行中重复相同的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆