2014-03-25 77 views
2

我使用apache FOP生成pdf文件。我有這樣的XSL代碼如何使用`xsl:include`與apache fop

<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="/"> 
    <fo:root font-size="11pt" font-family="serif"> 
     <fo:layout-master-set> 
     <fo:simple-page-master master-name="A4-portrait" 
      page-height="29.7cm" page-width="21.0cm" margin-top="1cm" 
      margin-left="1.5cm" margin-right="1cm" margin-bottom="1cm"> 
      <fo:region-body /> 
      <fo:region-after region-name="footer" extent="15mm"/> 
     </fo:simple-page-master> 
     <fo:simple-page-master master-name="A4-landscape" 
      page-width="29.7cm" page-height="21.0cm" margin-top="1cm" 
      margin-left="1cm" margin-right="1cm" margin-bottom="1cm"> 
      <fo:region-body /> 
      <fo:region-after region-name="footer2" display-align="after" extent="0cm"/> 
     </fo:simple-page-master> 
     </fo:layout-master-set> 
     <fo:page-sequence master-reference="A4-portrait"> 
     <xsl:include href="footer_common.xsl"/> 
     <fo:flow flow-name="xsl-region-body"> 
      .... 
     </fo:flow> 
     </fo:page-sequence> 
    </fo:root> 
    </xsl:template> 
</xsl:stylesheet> 

通知元素<xsl:include href="footer_common.xsl"/> 列入不工作! 這裏是footer_common.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    version="1.0"> 
    <fo:static-content flow-name="footer" font-size="7pt"> 
    <fo:table> 
     <fo:table-column column-width="70mm"/> 
     <fo:table-column column-width="70mm"/> 
     <fo:table-column column-width="70mm"/> 
     <fo:table-body> 
     ........... 
     </fo:table-body> 
    </fo:table> 
    </fo:static-content> 
</xsl:stylesheet> 

兩者的.xsl文件都在同一個資源目錄。如果它很重要 - 我使用eclipse進行開發。在我的java代碼中,我將主要的.xsl文件作爲資源流獲取並用於轉換。

的錯誤消息是 xsl:include ist an dieser Position in der Formatvorlage nicht zulässig! 這意味着,xsl:include is not allowed at that position in the template

有人能看到我在做什麼錯?

感謝您的任何幫助或提示。

回答

5

xsl:include是頂層元素(實際上是樣式表declaration),這意味着它可能只顯示爲xsl:stylesheet的直接子元素。因此,您無法在fo:page-sequence元素中包含樣式表。

但我認爲你不需要xsl:include和一個單獨的樣式表,但xsl:call-template和一個單獨的named template

寫類似下面的一個單獨的模板:

<xsl:template name="footer-ebase"> 
    <fo:static-content flow-name="footer" font-size="7pt"> 
    <fo:table> 
     <fo:table-column column-width="70mm"/> 
     <fo:table-column column-width="70mm"/> 
     <fo:table-column column-width="70mm"/> 
     <fo:table-body> 
     <!--...--> 
     </fo:table-body> 
    </fo:table> 
    </fo:static-content> 
</xsl:template> 

在主模板(您想插入內容的地方),引用該模板具有:

<fo:page-sequence master-reference="A4-portrait"> 
    <xsl:call-template name="footer-ebase"/> 
    <fo:flow flow-name="xsl-region-body"> 
    <!--...--> 
    </fo:flow> 
</fo:page-sequence> 

請注意,編寫命名模板並不總是有意義的。明智的做法是,如果

  • 你的代碼本來是多餘的,因爲你需要在幾個地方
  • 代碼會弄亂的模板,使相同的功能難以閱讀
  • 使用遞歸來解決問題

如果您想將內容拆分爲單獨的模板而沒有明顯的原因,那麼您最好不要使用它。

,您仍然可以把命名模板到一個單獨的樣式表,如果你願意,但你需要使用xsl:include作爲頂級元素

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
    <xsl:include href="footer-ebase.xsl"/> 
    <!--...--> 
</xsl:stylesheet>