2013-08-29 59 views
0

考慮下面的XML:如何在pdf生成之前預處理xsl:fo文件?

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

我要生成一本書的目錄。所以,我創建了TOC像這樣:

<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在文檔中還沒有被識別。因爲我使用相同的<xsl:attribute>元素來創建在cahpeter塊上設置章節標識。

<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已設置?

謝謝

回答

0

您不必做任何預處理。每一章都需要由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> 
+0

啊已經看到你的答案了,作品! – jorrebor