How to use `xsl:include` with apache fop

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

I use apache FOP to generate pdf files. I have this xsl code

<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> 

Notice the element <xsl:include href="footer_common.xsl"/> The inclusion does not work! And here is 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> 

Both .xsl files are in the same resource directory. If it matters - I use eclipse for development. In my java code I get the main .xsl file as a resource stream and use it for the transformation.

The error message is which means, xsl:include is not allowed at that position in the template

Can anyone see what I am doing wrong?

Thanks for any help or hints.

回答1:

xsl:include is a top-level element (actually, a stylesheet declaration), which means that it may only appear as an immediate child of xsl:stylesheet. So, you simply cannot include a stylesheet from within an fo:page-sequence element.

But I think you're not in need of xsl:include and a separate stylesheet, but of xsl:call-template and a separate named template.

Write a separate template similar to the following:

<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> 

In the main template (the place where you'd like to insert content), reference the named template with:

<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> 

Note that it does not always make sense to write named templates. It is advisable if

  • your code would otherwise be redundant because you need the same functionality in several places
  • the code would clutter the template and make it hard to read
  • you use recursion to solve a problem

If you want to split content into separate templates for no apparent reason, then you'd best do away with it alltogether.

You can still put the named template into a separate stylesheet if you wish, but then you need to use xsl:include as a top-level element:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!