Apache FOP:来自xml数组列表迭代的多页PDF

时间:2010-12-07 20:49:10

标签: java xml apache pdf apache-fop

我有xml文档,其中包含复杂元素的数组/列表。我想生成一个PDF文件,以便每个复杂元素显示在新页面上。我有一个页面工作。我的示例XML就像:

<root> <list><x><1>10</1><2>20</2></x> <x><1>11</1><2>22</2></x> <x><1>13</1><2>23</2></x></list> </root>

我试过以下

<xsl:for-each select="list/x">
<fo:page-sequence master-reference="my-page" initial-page-number="auto" format="1">
 <fo:flow flow-name="xsl-region-body">
 <fo:block> 
         ... my fo:table etc here
      </fo:block>
      <fo:block id="SV_RefID_PageTotal" />
</fo:page-sequence>
</xsl:for-each>

我收到验证错误。我究竟做错了什么?有更好的方法吗? 感谢

1 个答案:

答案 0 :(得分:0)

由于它们尚未发布,我假设您的错误是因为您的XML格式不正确。元素名称不能以数字开头。 (参见XML Elements)中的“XML命名规则”

我还做了一个快速的XSL样式表,它将生成有效的XSL-FO。它可能不是你想要的,但也许你可以使用它中的一两件?

修改后的示例XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <list>
    <x>
      <c1>10</c1>
      <c2>20</c2>
    </x>
    <x>
      <c1>11</c1>
      <c2>22</c2>
    </x>
    <x>
      <c1>13</c1>
      <c2>23</c2>
    </x>
  </list>
</root>

XSL样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:choose>
      <xsl:when test="name() = 'root'">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>        
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="list">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <xsl:apply-templates/>
    </fo:root>
  </xsl:template>

  <xsl:template match="x">
    <fo:page-sequence master-reference="my-page" initial-page-number="auto" format="1">
      <fo:flow flow-name="xsl-region-body">
        <fo:table>
          <fo:table-body>
            <fo:table-row>
              <xsl:apply-templates/>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>

  <xsl:template match="c1|c2">
    <fo:table-cell>
      <fo:block>
        <xsl:apply-templates/>
      </fo:block>
    </fo:table-cell>
  </xsl:template>

</xsl:stylesheet>

<强> XSL-FO

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page">
         <fo:region-body/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page" initial-page-number="auto" format="1">
      <fo:flow flow-name="xsl-region-body">
         <fo:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>10</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>20</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
   <fo:page-sequence master-reference="my-page" initial-page-number="auto" format="1">
      <fo:flow flow-name="xsl-region-body">
         <fo:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>11</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>22</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
   <fo:page-sequence master-reference="my-page" initial-page-number="auto" format="1">
      <fo:flow flow-name="xsl-region-body">
         <fo:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>13</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>23</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
</fo:root>