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

XSL-FO - Block and Inline Areas

Part:   1  2  3 

(Continued from previous part...)

XSL-FO Example - block.fo

Let's do some interesting tests on block and inline statements:

<?xml version="1.0" encoding="utf-8"?>
<!-- block.fo
     Copyright (c) 2002 by Dr. Herong Yang
-->
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <fo:layout-master-set>
  <fo:simple-page-master master-name="page" margin="0.5in">
   <fo:region-body/>
  </fo:simple-page-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="page">
  <fo:flow flow-name="xsl-region-body">
   <fo:block background-color="#ffffff">
    1. Pure text in a block statement.
   </fo:block>
   <fo:block background-color="#dfdfdf">
    <fo:inline>2. Pure </fo:inline>
    <fo:inline font-weight="bold">inline elements</fo:inline>
    <fo:inline> in a block statement.</fo:inline>
   </fo:block>
   <fo:block background-color="#ffffff">
    <fo:block>3. Pure </fo:block>
    <fo:block font-weight="bold">block elements</fo:block>
    <fo:block> in a block statement.</fo:block>
   </fo:block>
   <fo:block background-color="#dfdfdf">
    4. Text mixed with 
    <fo:inline font-weight="bold">inline elements</fo:inline>
     in a block statement.
   </fo:block>
   <fo:block background-color="#ffffff">
    5. Text mixed with 
    <fo:block font-weight="bold">block elements</fo:block>
     in a block statement.
   </fo:block>
   <fo:block background-color="#dfdfdf">
    <fo:inline>6. Inline elements mixed with </fo:inline>
    <fo:block font-weight="bold">block elements</fo:block>
    <fo:inline> in a block statement.</fo:inline>
   </fo:block>
   <fo:block background-color="#ffffff">
    <fo:inline>
     7. Text mixed with 
     <fo:inline font-weight="bold">inline elements</fo:inline>
      in a block statement.
     </fo:inline>
   </fo:block>
   <fo:block background-color="#dfdfdf">
    <fo:inline>
     8. Text mixed with 
     <fo:block font-weight="bold">block elements</fo:block>
      in an inline statement.
    </fo:inline>
   </fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>

If you use FOP to convert simple_page.fo to PDF:

run_fop -fo block.fo -pdf block.pdf

Print block.pdf, and you will see:

    1. Pure text in a block statement.
    2. Pure inline elements in a block statement.
    3. Pure 
    block elements
    in a block statement.
    4. Text mixed with inline elements in a block statement.
    5. Text mixed with in a block statement.
    block elements
    5. Text mixed with in a block statement.
    6. Inline elements mixed with in a block statement.
    block elements
    6. Inline elements mixed with in a block statement.
    7. Text mixed with inline elements in a block statement.
    8. Text mixed with in an inline statement.
    block elements
    8. Text mixed with in an inline statement.

Obviously, there is a problem with mixing text or inline areas with block areas. All texts and inline areas are collected and concatenated together before processing the all sub nodes (text, inline and block areas are sub nodes from the XML point of view). While stacking areas, a block area will break the current inline stack, and start a new block; a text node will be replaced by the concatenated version.

If you don't want to interrupt the inline stacking process, use the "wrapper" statement. For example:

   <fo:block background-color="#ffffff">
    Text can be safely mixed with 
    <fo:wrapper font-weight="bold">wrapper elements</fo:wrapper>
     in a block statement.
   </fo:block>

(Continued on next part...)

Part:   1  2  3 

杨和荣,修改于2007年
和荣笔记 - XSL-FO 与 XHTML - XSL-FO - Block and Inline Areas