如何在apache fop中使用`xsl:include`

use*_*745 2 xslt xsl-fo transformation 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>
Run Code Online (Sandbox Code Playgroud)

注意元素<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>
Run Code Online (Sandbox Code Playgroud)

两个.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

谁能看到我做错了什么?

感谢您的帮助或提示.

Mat*_*ler 5

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>
Run Code Online (Sandbox Code Playgroud)

在主模板(您要插入内容的位置)中,使用以下命令引用命名模板:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,编写命名模板并不总是有意义的.建议如果

  • 否则你的代码将是多余的,因为你需要在几个地方使用相同的功能
  • 代码会使模板混乱并使其难以阅读
  • 你使用递归来解决问题

如果你想将内容分成不同的模板,那么你最好完全取消它.

如果愿意,您仍然可以将命名模板放入单独的样式表中,但是您需要将其xsl:include用作顶级元素:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:include href="footer-ebase.xsl"/>
    <!--...-->
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)