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

XSL-FO 简介

Part:   1  2 

(Continued from previous part...)

将 XSL-FO 文件排版生成 .txt 文件

首先让我们作一个简单的例子,将 hello.fo 排版生成一个 .txt 文件。请运行下面的指令:

run_fop -fo hello.fo -txt hello.txt

你会得到如下的信息:

[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] FOP 0.20.5rc2
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] building formatting object tree
[INFO] setting up fonts
[INFO] rendering areas to TEXT
[INFO] [1]
[INFO] Parsing of document complete, stopping renderer
[INFO] writing out TEXT

从这些信息中,我们可以看出,FOP 使用了 Xerces SAXParser 来阅读 XSL-FO 文件。我们 还可以看出,排版后的结果只有一页。

为了验证结果,请将 hello.txt 文件用 notepad 打开,你会看到:




        Hello  world!

...

排版结果完全正确。

将 XSL-FO 文件排版生成 .pdf 文件

我们的第二个例子是将 hello.fo 排版生成一个 .pdf 文件。请运行下面的指令:

run_fop -fo hello.fo -pdf hello.pdf

你会得到如下的信息:

[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] FOP 0.20.5rc2
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] building formatting object tree
[INFO] setting up fonts
[INFO] [1]
[INFO] Parsing of document complete, stopping renderer

然后请用 Adobe Reader 打开 hello.pdf,你会看到“Hello world!”被正确地排版在 页面的左上角。这个结果非常鼓舞人心,我对 FOP 的感觉越来越好了。

将 XML 文件转换排版生成 .pdf 文件

在以上两个例子中,我们将原始文字资料已经和排版语句结合在一起了。在这个例子里, 我们将使用两个输入文件:一个是原始文字资料文件 hello_xsl.xml,一个是转换和排版 语句文件 hello_fo.xls。

hello_xsl.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<p>Hello world!</p>

hello_fo.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="p">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <fo:layout-master-set>
  <fo:simple-page-master master-name="my_page" margin="0.5in">
   <fo:region-body/>
  </fo:simple-page-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="my_page">
  <fo:flow flow-name="xsl-region-body">
   <fo:block>
   -<xsl:value-of select="."/>-
   </fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>
 </xsl:template>
</xsl:stylesheet>

请注意:

  • 排版指令“fo:*”被嵌入在转换指令“xsl:*”之中。
  • 转换指令除了引入了排版指令以外,还将原文字资料作了简单的转换,在其首尾各加了 一字符:“-”。

现在请运行下面的指令:

run_fop -xml hello_xsl.xml -xsl hello_fo.xsl -pdf hello.pdf

如果你打开 hello.pdf,转换后的文字资料“-Hello world!-”被正确的排版在页面的左上角。

XML 文件的转换处理

在 FOP 软件包里,还有一个 XML 文件处理软件:“xalan”。 它的功能是对 XML 文件作转换处理。下面是 xalan 的运行指令文件,run_xalan.bat:

\local\j2sdk1.4.1_01\bin\java 
   -cp \local\fop-0.20.5rc2\lib\xercesImpl-2.2.1.jar;
   \local\fop-0.20.5rc2\lib\xalan-2.4.1.jar 
   org.apache.xalan.xslt.Process %1 %2 %3 %4 %5 %6

让我们借用上面最后一个例子来试验,使用 xalan,安照 hello_fo.xml 来转换 hello_xsl.xml:

run_xalan -in hello_xsl.xml -xsl hello_fo.xsl -out hello.out

下面是转换后的结果 hello.out:

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"><fo:layout...
   -Hello world!-
   </fo:block></fo:flow></fo:page-sequence></fo:root>

这个结果和我们的预计是一致的。排版语句被完整地保留下来了,原文字资料也被作过转换。

结论一:XML 文件用于组织原始文字资料,XSL 文件用于定义资料转换规则。如果需要, 转换规则内部还可以嵌入排版规则,用以控制资料的显示和打印。

结论二:FOP 软件可以用于资料转换处理,可以用于资料排版处理,还可以用于资料转换和 排版的一次性处理。

Part:   1  2 

杨和荣,修改于2007年
和荣笔记 - XSL-FO 与 XHTML - XSL-FO 简介