[英]How to use `xsl:include` with apache fop
我使用apache FOP生成pdf文件。 我有這個XSL代碼
<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
誰能看到我在做什么錯?
感謝您的幫助或提示。
xsl:include
是頂級元素(實際上是樣式表聲明 ),這意味着它只能作為xsl:stylesheet
的直接子代出現。 因此,您根本無法在fo:page-sequence
元素中包含樣式表。
但我認為您不需要xsl:include
和單獨的樣式表,而需要xsl:call-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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.