【问题标题】:How to preprocess xsl:fo document before pdf generation?如何在生成 pdf 之前预处理 xsl:fo 文档?
【发布时间】:2013-09-01 18:11:14
【问题描述】:

考虑以下 xml:

 <book>
    <chapter>
            <title>chapter title 1</title>
        <id>chapterId1</id>
        <content>some content</content> 
    </chapter>
    <chapter>
            <title>chapter title 2</title>
        <id>chapterId2</id>
        <content>some content</content> 
    </chapter>
</book>

我想生成一本带有目录的书。所以我像这样创建目录:

<xsl:for-each select="book/chapter">
    <fo:block>
        <xsl:value-of select="title" />
            <fo:page-number-citation ref-id="id" >
                <xsl:attribute name="ref-id">
                    <xsl:value-of select="id" />
                </xsl:attribute>
            </fo:page-number>
    </fo:block>
</xsl:for-each>

ref-id 属性会被即时覆盖并替换为章节 ID 的值。

这里的问题是引用的 id 在文档中还不知道。因为我使用相同的&lt;xsl:attribute&gt; 元素在 cahpeter 块上创建设置章节 ID。

<fo:block> <!-- the chapter container -->
  <xsl:attribute name="id">
    <xsl:value-of select="chapter/id" />
  </xsl:attribute>
  <fo:block>
      the chapter content.....
  </fo:block>
</fo:block>

如何预处理我的 xsl:fo 文件,以便 fo:block 元素已设置正确的 ID?

谢谢

【问题讨论】:

    标签: xml xslt xml-parsing xsl-fo


    【解决方案1】:

    您不必进行任何预处理。每章都需要由 XSLT 样式表处理两次:生成主要内容时和生成 TOC 时。只需确保在两种情况下都使用相同的 ID。

    以下样式表说明了如何做到这一点(改编自this answer)。它适用于您的示例源文档。

    <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 indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:template>
    
      <xsl:template match="/book">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
           <fo:layout-master-set>
              <fo:simple-page-master master-name="my-page" page-width="8.5in" 
                                     page-height="11in">
                 <fo:region-body margin="1in" margin-top="1.5in"/>
              </fo:simple-page-master>
           </fo:layout-master-set>
           <fo:page-sequence master-reference="my-page">
             <fo:flow flow-name="xsl-region-body">
             <!-- Call the template that generates the TOC -->
               <xsl:call-template name="genTOC"/>
             </fo:flow>
            </fo:page-sequence>
            <!-- Then output the main content -->
            <xsl:apply-templates/>
        </fo:root>
      </xsl:template>
    
      <xsl:template name="genTOC">
        <fo:block break-before='page'>
          <fo:block font-size="16pt" font-weight="bold">TABLE OF CONTENTS</fo:block>
          <xsl:for-each select="chapter">
            <fo:block text-align-last="justify">
                <xsl:value-of select="count(preceding::chapter) + 1" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="title" />
                <fo:leader leader-pattern="dots" />
                <fo:page-number-citation ref-id="{id}" />
            </fo:block>
          </xsl:for-each>
        </fo:block>
      </xsl:template>
    
      <xsl:template match="title|content">
        <fo:block><xsl:value-of select="."/></fo:block>
      </xsl:template>
    
      <xsl:template match="chapter">
        <fo:page-sequence master-reference="my-page" id="{id}">
           <fo:flow flow-name="xsl-region-body">
             <xsl:apply-templates/>
           </fo:flow>
        </fo:page-sequence>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 2011-03-28
      • 2012-08-27
      • 2014-09-09
      • 2021-01-28
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多