本文介绍了如何在Apache FOP中使用`xsl:include`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用apache FOP生成pdf文件.我有这个xsl代码

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>

注意元素<xsl:include href="footer_common.xsl"/>包含不起作用!这是footer_common.xsl:

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>

两个.xsl文件都在同一资源目录中.如果重要的话-我使用Eclipse进行开发.在我的Java代码中,我将主.xsl文件作为资源流获取并用于转换.

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.

错误消息是xsl:include ist an dieser Position in der Formatvorlage nicht zulässig!这表示xsl:include is not allowed at that position in the template

The error message isxsl:include ist an dieser Position in der Formatvorlage nicht zulässig!which means, xsl:include is not allowed at that position in the template

有人可以看到我在做什么错吗?

Can anyone see what I am doing wrong?

感谢您的帮助或提示.

推荐答案

xsl:include是顶层元素(实际上是样式表声明),这意味着它只能作为xsl:stylesheet的直接子代出现.因此,您根本无法在fo:page-sequence元素中包含样式表.

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.

但是我认为您不需要xsl:include和单独的样式表,而是需要xsl:call-template和单独的命名模板.

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

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

如果您想将内容拆分为单独的模板而没有明显的原因,那么最好将其全部删除.

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

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

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>

这篇关于如何在Apache FOP中使用`xsl:include`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:49