XSLT - 生成地址标签

时间:2011-05-03 19:40:20

标签: xslt xsl-fo apache-fop

更新重新提出问题以澄清混淆。

我正在使用Apache FOP翻译的XSLT和XSL:FO。我想打印地址标签。

INPUT

<?xml version="1.0" encoding="utf-8" ?>
<workOrders>
  <workOrder>
    <number>111</number>
    <PartNumber>110022</PartNumber>
    <col3>222</col3>
    <Qty>333</Qty>
  </workOrder>
  <workOrder>
    <number>111</number>
    <PartNumber>110022</PartNumber>
    <col3>222</col3>
    <Qty>333</Qty>
  </workOrder>

  <!--Manually copy/paste the workOrder until you have 47 of them..-->
</workOrders>

输出
第1页(整页6行x 3列)
Page1

第2页与第1页相同。

第3页(部分页面...在这种情况下为4行x 3列,最后一项为空白)

Page3

<小时/> 的 UPDATE2
我插入了亚历杭德罗的解决方案。我收到Apache FOP报告的错误

  

xsl:模板不允许在样式表中的此位置

这是从HTML内容转换为XSL:FO的代码。错误点由评论标记。我搞砸了什么?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <fo:layout-master-set>

      <!-- layout for the first page -->
      <fo:simple-page-master master-name="first"
            page-height="11in"
            page-width="8.5in"
            margin-top="1cm"
            margin-bottom="1cm"
            margin-left="1cm"
            margin-right="1cm">
        <fo:region-body margin-top="0cm"/>
        <fo:region-before extent="1cm"/>
        <fo:region-after extent="0cm"/>
      </fo:simple-page-master>

      <!-- layout for the other pages -->
      <fo:simple-page-master master-name="rest"
            page-height="11in"
            page-width="8.5in"
            margin-top="1cm"
            margin-bottom="1cm"
            margin-left="1cm"
            margin-right="1cm">
        <fo:region-body margin-top="0cm"/>
        <fo:region-before extent="1cm"/>
        <fo:region-after extent="0cm"/>
      </fo:simple-page-master>

      <fo:page-sequence-master master-name="basicPSM" >
        <fo:repeatable-page-master-alternatives>
          <fo:conditional-page-master-reference master-reference="first" page-position="first" />
          <fo:conditional-page-master-reference master-reference="rest" page-position="rest" />
          <!-- recommended fallback procedure -->
          <fo:conditional-page-master-reference master-reference="rest" />
        </fo:repeatable-page-master-alternatives>
      </fo:page-sequence-master>

    </fo:layout-master-set>
    <!-- end: defines page layout -->

    <!-- actual layout -->
    <fo:page-sequence master-reference="basicPSM">

      <fo:flow flow-name="xsl-region-body">


  <xsl:template match="/" name="tables"><!--ERROR REFERS TO HERE-->
    <xsl:param name="pRows" select="3"/>
    <xsl:param name="pColumns" select="3"/>
    <xsl:param name="pSequence" select="*/*"/>
    <xsl:variable name="vSize" select="$pRows * $pColumns"/>
    <xsl:for-each select="$pSequence[position() mod $vSize = 1]">
      <xsl:variable name="vPosition" select="position()"/>
      <fo:table table-layout="fixed" width="63mm" border-collapse="separate" wrap-option="wrap">
        <fo:table-body wrap-option="wrap">
          <xsl:call-template name="rows">
            <xsl:with-param name="pSequence"
                 select="$pSequence[
                                    position() > ($vPosition - 1) * $vSize
                                     and
                                    $vPosition * $vSize + 1 > position()
                                 ]"/>
          </xsl:call-template>
        </fo:table-body>
      </fo:table>
    </xsl:for-each>
  </xsl:template>


  <xsl:template name="rows">
    <xsl:param name="pSequence" select="/.."/>
    <xsl:param name="pRow" select="$pRows"/>
    <xsl:if test="$pRow">
      <xsl:call-template name="rows">
        <xsl:with-param name="pSequence" select="$pSequence"/>
        <xsl:with-param name="pRow" select="$pRow - 1"/>
      </xsl:call-template>
      <fo:table-row wrap-option="wrap">
        <xsl:call-template name="columns">
          <xsl:with-param name="pSequence"
               select="$pSequence[
                                    position() > ($pRow - 1) * $pColumns
                                     and
                                    $pRow * $pColumns + 1 > position()
                                 ]"/>
        </xsl:call-template>
      </fo:table-row>
    </xsl:if>
  </xsl:template>

  <xsl:template name="columns">
    <xsl:param name="pSequence" select="/.."/>
    <xsl:param name="pColumn" select="$pColumns"/>
    <xsl:if test="$pColumn">
      <xsl:call-template name="columns">
        <xsl:with-param name="pSequence" select="$pSequence"/>
        <xsl:with-param name="pColumn" select="$pColumn - 1"/>
      </xsl:call-template>
      <fo:table-cell width="90mm">
        <fo:block wrap-option="wrap">
          <xsl:apply-templates select="$pSequence[$pColumn]"/>
        </fo:block>
      </fo:table-cell>
    </xsl:if>
  </xsl:template>

  <xsl:output method="xml"/>
  <xsl:template match="/">
    <xsl:call-template name="tables">
      <xsl:with-param name="pSequence" select="workOrders/workOrder[position()!=1]"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="workOrder">
    <xsl:value-of select="PartNumber"/>
  </xsl:template>

  </fo:flow>
  </fo:page-sequence>
  </fo:root>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:3)

这个XSLT 1.0样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pRows" select="6"/>
    <xsl:param name="pColumns" select="3"/>
    <xsl:variable name="pCells" select="$pRows * $pColumns"/>
    <xsl:template match="workOrders">
        <xsl:apply-templates
             select="workOrder[position() mod $pCells = 1]"
             mode="table"/>
    </xsl:template>
    <xsl:template match="workOrder" mode="table">
        <table>
            <xsl:apply-templates
                 select="(.|following-sibling::workOrder
                               [$pCells > position()])
                            [position() mod $pColumns = 1]"
                 mode="row"/>
        </table>
    </xsl:template>
    <xsl:template match="workOrder" mode="row">
        <tr>
            <xsl:apply-templates
                 select="(.|following-sibling::workOrder
                               [$pColumns > position()])"
                 mode="cell"/>
        </tr>
    </xsl:template>
    <xsl:template match="workOrder" mode="cell">
        <td>
            <xsl:value-of select="PartNumber"/>
        </td>
    </xsl:template>
</xsl:stylesheet>

输出(47 workOrder s):

<table>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
</table>
<table>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
</table>
<table>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
        <td>110022</td>
    </tr>
    <tr>
        <td>110022</td>
        <td>110022</td>
    </tr>
</table>

答案 1 :(得分:1)

如果您知道订单不超过18个,您可以使用以下内容:

<xsl:if test="Order">
  <tableRow>
    <xsl:apply-templates select="Order[position() &lt;= 6]" />
  </tableRow>
  <xsl:if test="Order[7]">
    <tableRow>
      <xsl:apply-templates select="Order[position() &lt;= 12 and position() > 6]" />
    </tableRow>
    <xsl:if test="Order[13]">
      <tableRow>
        <xsl:apply-templates select="Order[position() &lt;= 18 and position() > 12]" />
      </tableRow>
    </xsl:if>
  </xsl:if>
</xsl:if>

然后你有一个匹配"Order"的模板并输出一个表格单元格。

不是最优雅,但很容易。

更新:

好的,发现以上不是OP想要的。

以下是重新提问的问题的部分解决方案。免责声明:

  1. 我不知道FO,所以我将该部分留作伪代码。从问题标题来看,我认为你想要帮助的部分只是XSLT。

  2. 我不确定您是否需要有关页面总体结构或每个单元格格式或两者的答案,我已经解决了前者而不是后者。除非我从你那里听到你的需要,否则我会不会花时间在后者身上。再一次,不知道FO,我不知道你是否可以将每个单元格的内容格式化为嵌入式表格,这是排列其列的最简单方法......

  3. 无论如何,这是部分解决方案:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       exclude-result-prefixes="xs"
       version="2.0">
       <xsl:variable name="rowsPerPage" select="6"/>
       <xsl:variable name="columns" select="3"/>
       <xsl:variable name="cellsPerPage" select="$rowsPerPage * $columns"/>
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="workOrders">
          <xsl:for-each-group select="workOrder"
                   group-by="position() idiv $cellsPerPage">
             <!-- <page> is pseudocode for FO markup -->
             <page>
                <xsl:for-each-group select="current-group()"
                         group-by="position() idiv $columns">
                   <!-- <row> is pseudocode for FO markup -->
                   <row>
                      <xsl:apply-templates select="current-group()" mode="cell"/>
                   </row>
                </xsl:for-each-group> 
             </page>
          </xsl:for-each-group> 
       </xsl:template>
    
       <xsl:template match="workOrder" mode="cell">
          <!-- <cell> is pseudocode for FO markup -->
          <cell>
             <!-- here goes code for laying out the address label -->
          </cell>
       </xsl:template>
    </xsl:stylesheet>