2013-07-09 122 views
2

我嘗試使用Apache FOP使用XML數據和XSL樣式表來創建PDF,但我不斷收到以下錯誤不能與Apache FOP

org.apache.fop.apps生成PDF。 FOPException: org.apache.fop.fo.ValidationException: 「{http://www.w3.org/1999/XSL/Format} block」不是 「fo:root」的有效子項! (沒有可用的上下文信息) javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: 「{http://www.w3.org/1999/XSL/Format} block」不是 的有效子項「fo:root」! (沒有可用的上下文信息)

奇怪的是,它在我的本地Windows機器上測試時工作正常,但是當我試圖在Debian服務器上執行相同的操作時,出現此錯誤。

我做的命令行

fop -c fop_config.xml -xml /tmp/fop4IwJKZ -xsl stylesheet.xsl -pdf pdf.pdf 

下面就下面是我的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<report> 
    <heading>Heading</heading> 
    <mailersent>18/06/2013 14:23</mailersent> 
    <reportgenerated>18/06/2013 17:26</reportgenerated> 
    <portrait> 
     <title>Summary</title> 
     <graph>graphs/graph1.png</graph> 
     <stats> 
      <stat> 
       <value>16.67%</value> 
       <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor</text> 
      </stat> 
      <stat> 
       <value>2.25%</value> 
       <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor</text> 
      </stat> 
      <stat> 
       <value>13.49%</value> 
       <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor through</text> 
      </stat> 
      <stat> 
       <value>8.99%</value> 
       <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor</text> 
      </stat> 
     </stats> 
     </portrait> 
</report> 

樣式表:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="node()|@*"> 
    <xsl:apply-templates select="node()|@*"/> 
</xsl:template> 

<xsl:template match="/report"> 
    <fo:root> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="portraitLayout" page-height="29.7cm" page-width="21.0cm" margin="1cm"> 
       <fo:region-body margin-bottom="1cm"/> 
       <fo:region-after extent="1cm"/> 
      </fo:simple-page-master> 
      <fo:simple-page-master master-name="landscapeLayout" page-height="21cm" page-width="30cm" margin="1cm"> 
       <fo:region-body margin-bottom="1cm"/> 
       <fo:region-after extent="1cm"/> 
      </fo:simple-page-master> 
     </fo:layout-master-set> 

     <fo:page-sequence master-reference="portraitLayout"> 
      <fo:flow flow-name="xsl-region-body"> 
       <xsl:call-template name="heading"/> 
      </fo:flow> 
     </fo:page-sequence> 

     <fo:page-sequence master-reference="portraitLayout"> 
      <fo:flow flow-name="xsl-region-body"> 
       <xsl:call-template name="genTOC"/> 
      </fo:flow> 
     </fo:page-sequence> 
     <xsl:apply-templates/> 
    </fo:root> 
</xsl:template> 

<xsl:template name="genTOC"> 
    <fo:block break-before='page'> 
     <fo:block margin-bottom="1cm" font-size="16pt" font-weight="bold" text-align="center">Contents</fo:block> 
     <xsl:for-each select="//portrait|//landscape"> 
      <fo:block text-align-last="justify"> 
       <fo:basic-link internal-destination="{generate-id(.)}"> 
        <xsl:value-of select="title" /> 
        <fo:leader leader-pattern="dots" /> 
        <fo:page-number-citation ref-id="{generate-id(.)}" /> 
       </fo:basic-link> 
      </fo:block> 
     </xsl:for-each> 
    </fo:block> 
</xsl:template> 

<xsl:template match="portrait"> 
    <fo:page-sequence master-reference="portraitLayout" id="{generate-id(.)}"> 
     <fo:static-content flow-name="xsl-region-after"> 
      <fo:block font-size="8pt" text-align="center" border-top-color="#000000" border-top-style="solid" border-top-width=".3mm" padding-top="0.2cm"> 
       Page <fo:page-number/> of 9 
      </fo:block> 
     </fo:static-content> 
     <fo:flow flow-name="xsl-region-body"> 
      <xsl:apply-templates/> 
     </fo:flow> 
    </fo:page-sequence> 
</xsl:template> 

<xsl:template match="landscape"> 
    <fo:page-sequence master-reference="landscapeLayout" id="{generate-id(.)}"> 
     <fo:static-content flow-name="xsl-region-after"> 
      <fo:block font-size="8pt" text-align="center" border-top-color="#000000" border-top-style="solid" border-top-width=".3mm" padding-top="0.2cm"> 
       Page <fo:page-number/> of 9 
      </fo:block> 
     </fo:static-content> 
     <fo:flow flow-name="xsl-region-body"> 
      <xsl:apply-templates/> 
     </fo:flow> 
    </fo:page-sequence> 
</xsl:template> 

<xsl:template name="heading"> 
    <fo:block margin-top="10cm" font-size="21pt" font-weight="bold" text-align="center"> 
     <xsl:value-of select="heading"/> 
    </fo:block> 
    <fo:block margin-top="0.3cm" font-size="16pt" text-align="center"> 
     <xsl:text>Mailer sent: </xsl:text> 
     <xsl:value-of select="mailersent"/> 
    </fo:block> 
    <fo:block margin-top="0.3cm" font-size="16pt" text-align="center"> 
     <xsl:text>Report generated: </xsl:text> 
     <xsl:value-of select="reportgenerated"/> 
    </fo:block> 
</xsl:template> 

<xsl:template match="title"> 
    <fo:block margin-bottom="0.5cm" border-bottom-color="#000000" border-bottom-style="solid" border-bottom-width=".5mm" font-size="20pt"> 
     <xsl:value-of select="."/> 
    </fo:block> 
</xsl:template> 

<xsl:template match="graph"> 
    <fo:block margin-bottom="0.5cm" border-color="#d8d8d8" border-style="solid" border-width=".3mm" font-size="10pt"> 
     <xsl:variable name="graphimage"> 
      <xsl:value-of select="."/> 
     </xsl:variable> 
     <fo:external-graphic src="{$graphimage}" content-height="scale-to-fit" content-width="16cm" scaling="non-uniform"/> 
    </fo:block> 
</xsl:template> 

<xsl:template match="svggraph"> 
    <fo:block margin-bottom="0.5cm" border-color="#d8d8d8" border-style="solid" border-width=".3mm" font-size="10pt"> 
     <fo:instream-foreign-object xmlns:svg="http://www.w3.org/2000/svg" content-width="19cm" content-height="5cm"> 
     <svg:svg width="25" height="25"> 
     <svg:g style="fill:red; stroke:#000000"> 
     <svg:rect x="0" y="0" width="15" height="15"/> 
     <svg:rect x="5" y="5" width="15" height="15"/> 
     </svg:g> 
     </svg:svg> 
     </fo:instream-foreign-object> 
    </fo:block> 
</xsl:template> 

<xsl:template match="stats"> 
    <xsl:for-each select="stat"> 
     <fo:block margin-bottom="0.5cm" font-size="10pt"> 
      <fo:inline font-weight="bold" font-size="12pt"> 
       <xsl:value-of select="value"/> 
      </fo:inline> 
      <xsl:text> </xsl:text> 
      <xsl:value-of select="text"/> 
     </fo:block> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="table"> 
    <fo:block> 
     <fo:table margin-bottom="0.5cm" border-collapse="separate" border-color="#d8d8d8" border-style="solid" border-width=".3mm" table-layout="fixed" width="100%" font-size="10pt"> 

      <xsl:for-each select="thead/tr/th"> 
       <xsl:choose> 
        <xsl:when test="width"> 
         <xsl:variable name="columnwidth"> 
          <xsl:value-of select="width" /> 
         </xsl:variable> 
         <fo:table-column column-width="{$columnwidth}"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <fo:table-column/> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each> 

      <fo:table-header background-color="#ededed">      
       <xsl:for-each select="thead/tr/th"> 
        <fo:table-cell padding-top="5pt" padding-bottom="5pt" padding-left="3pt" padding-right="3pt"> 
         <fo:block font-weight="bold"> 
          <xsl:value-of select="heading"/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each>     
      </fo:table-header> 
      <fo:table-body>     
       <xsl:for-each select="tbody/tr">       
        <xsl:variable name="row-background"> 
         <xsl:choose> 
          <xsl:when test="position() mod 2 = 0">#f8f8f8</xsl:when> 
          <xsl:otherwise>#ffffff</xsl:otherwise> 
         </xsl:choose> 
        </xsl:variable>      
        <fo:table-row background-color="{$row-background}">       
         <xsl:for-each select="td"> 
          <fo:table-cell padding-top="5pt" padding-bottom="5pt" padding-left="3pt" padding-right="3pt"> 
           <fo:block> 
            <xsl:value-of select="."/> 
           </fo:block> 
          </fo:table-cell> 
         </xsl:for-each>       
        </fo:table-row>      
       </xsl:for-each> 
      </fo:table-body> 
     </fo:table> 
    </fo:block> 
</xsl:template> 

<xsl:template match="text"> 
    <fo:block font-size="10pt"> 
     <xsl:value-of select="."/> 
    </fo:block> 
</xsl:template> 

</xsl:stylesheet> 

更新:我已經檢查了XSLT處理上兩臺機器都是一樣的。我也將兩臺機器上的XML + XSL轉換爲XLS:FO,並且這兩個文件都是相同的。

+0

我不能重現使用XML和樣式表的誤差。在生成的FO文件中沒有立即的'root'子塊。 – mzjn

+0

@mzjn我知道這就是爲什麼它看起來很奇怪的錯誤。我會繼續嘗試 – Pattle

+0

每個平臺上使用的兩個XSLT過程是什麼?我敢打賭,他們是不同的。在每個平臺上將XML + XSL格式化爲XSL FO並比較差異。 –

回答

1

我用

C:\Users\User>cd Documents 
C:\Users\User\Documents>cd fop-1.0 
C:\Users\User\Documents\fop-1.0>fop -xsl stovfl17548663.xsl -xml stovfl17548663.xml -pdf stovfl17548663.pdf 

您的XML和XSL文件,我得到了以下信息

C:\Users\User\Documents\fop-1.0>fop -xsl stovfl17548663.xsl -xml stovfl17548663.xml -pdf stovfl17548663.pdf 
Sep 26, 2013 12:09:47 PM org.apache.fop.events.LoggingEventListener processEvent 
WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400". 
Sep 26, 2013 12:09:47 PM org.apache.fop.events.LoggingEventListener processEvent 
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400". 
Sep 26, 2013 12:09:48 PM org.apache.fop.events.LoggingEventListener processEvent 
SEVERE: Image not found. URI: graphs/graph1.png. (No context info available) 
Sep 26, 2013 12:09:48 PM org.apache.fop.events.LoggingEventListener processEvent 
SEVERE: Image not found. URI: graphs/graph1.png. (No context info available) 
Sep 26, 2013 12:09:48 PM org.apache.fop.events.LoggingEventListener processEvent 
WARNING: 1 link target could not be fully resolved and now point to the top of the page or is dysfunctional. 

The resulting PDF