和荣笔记 - XSL-FO 与 XHTML
杨和荣, 版本 2.03, 2007年

XHTML Publication - From Web to PDF

Part:   1  2  3  4 

(Continued from previous part...)

      <fo:list-item-body>
       <fo:block font-weight="normal" text-align="end">
       </fo:block>
      </fo:list-item-body>
     </fo:list-item>
    </fo:list-block>
   </fo:block>
  </fo:static-content>
  <fo:flow flow-name="body"
   font-family="serif" font-size="12pt" font-style="normal">
  <xsl:apply-templates select="body"/>
  </fo:flow>
 </fo:page-sequence>
</fo:root>
 </xsl:template>
 <xsl:template match="body">
  <xsl:apply-templates select="//p/ul|//p|//ul|//pre"/>
 </xsl:template>
 <xsl:template match="p[@class='chapter_title']">
   <fo:block id="{@id}" font-size="150%" font-weight="bold"
    space-before="12pt" space-after="4pt">
  <xsl:apply-templates select="text()"/>
   </fo:block>
 </xsl:template>
 <xsl:template match="p[@class='section_title']">
   <fo:block font-size="125%" font-weight="bold"
    space-before="9pt" space-after="4pt">
  <xsl:apply-templates select="text()"/>
   </fo:block>
 </xsl:template>
 <xsl:template match="a">
   <fo:basic-link external-destination="url('{@href}')" color="#0000ff">
  <xsl:apply-templates select="text()"/>
   </fo:basic-link>
 </xsl:template>
 <xsl:template match="b">
   <fo:wrapper font-weight="bold">
  <xsl:apply-templates select="text()"/>
   </fo:wrapper>
 </xsl:template>
 <xsl:template match="i">
   <fo:wrapper font-style="italic">
  <xsl:apply-templates select="text()"/>
   </fo:wrapper>
 </xsl:template>
 <xsl:template match="p[@class='toc_item']">
   <fo:block space-before="8pt" space-after="4pt"
    text-align-last="justify">
  <xsl:apply-templates select="a|text()"/>
   </fo:block>
 </xsl:template>
 <xsl:template match="p">
   <fo:block space-before="8pt" space-after="4pt">
  <xsl:apply-templates select="a|b|i|text()"/>
   </fo:block>
 </xsl:template>
 <xsl:template match="text()">
  <xsl:value-of select="."/>
 </xsl:template>
 <xsl:template match="pre[@class='block_source']">
    <fo:block font-family="monospace" font-size="10pt" 
     background-color="#cfcfcf"
     white-space-collapse="false" wrap-option="no-wrap">
  <xsl:value-of select="./text()"/>
    </fo:block>
 </xsl:template>
 <xsl:template match="pre[@class='block_bold']">
    <fo:block font-family="monospace" font-size="10pt" 
     background-color="#cfcfcf" font-weight="bold"
     white-space-collapse="false" wrap-option="no-wrap">
  <xsl:value-of select="./text()"/>
    </fo:block>
 </xsl:template>
 <xsl:template match="pre[@class='block_syntax']">
    <fo:block font-family="monospace" font-size="10pt" 
     background-color="#cfcfcf" font-style="italic"
     white-space-collapse="false" wrap-option="no-wrap">
  <xsl:value-of select="./text()"/>
    </fo:block>
 </xsl:template>
 <xsl:template match="//p/ul">
    <fo:list-block space-before="8pt" space-after="4pt">
  <xsl:apply-templates select="li"/>
    </fo:list-block>
 </xsl:template>
 <xsl:template match="ul">
    <fo:list-block space-before="1pt" space-after="1pt">
  <xsl:apply-templates select="li"/>
    </fo:list-block>
 </xsl:template>
 <xsl:template match="li">
    <fo:list-item>
     <fo:list-item-label start-indent="0.1in">
     <fo:block>•</fo:block>
     </fo:list-item-label>
     <fo:list-item-body start-indent="0.25in">
     <fo:block space-after="3pt">
  <xsl:apply-templates select="a|b|i|ul|text()"/>
     </fo:block>
     </fo:list-item-body>
    </fo:list-item>
 </xsl:template>
</xsl:stylesheet>

Note that:

  • XPath "//p" is used to by-pass the layers to tables used for the Web page setup.
  • My paragraph level blocks are "//p|//ul|//pre", so do not misuse them.
  • Margins and spaces can be easily adjusted. But watch out "//pre" blocks.
  • Only a limited number of xhtml tags are supported here. But it's easy to add more to it.

Process Instructions

  • Copy *.xhtml and book.css into a directory on Web server. Web publishing is done.
  • Run "xalan -in *.xhtml -xsl book_fo.xsl -out *.fo". This will produce one FO file per chapter.
  • Open toc.fo, append at the end with "page-sequence" elements from other *.fo files, and name the final file as toc_expanded.fo.
  • Run "fop toc_expanded.fo toc_expanded.pdf". PDF pusblishing is done.
  • Of course, I have automated this process with a perl program.

Part:   1  2  3  4 

杨和荣,修改于2007年
和荣笔记 - XSL-FO 与 XHTML - XHTML Publication - From Web to PDF