|
XSL Formatting Objects (XSL-FO)
Part:
1
2
(Continued from previous part...)
Formatting an XSL-FO File to a .txt File
Let's format hello.fo into a .txt file with the following command:
run_fop -fo hello.fo -txt hello.txt
You should see the following on the screen:
[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
The output tells us that the xerces SAXParser was used to parse the XSL-FO file,
and one page of output was formatted.
If you open the output file, hello.txt, with any text editor, you should see:
Hello world!
...
This is perfect.
Formatting an XSL-FO File to a .pdf File
Let's format hello.fo into a .pdf file with the following command:
run_fop -fo hello.fo -pdf hello.pdf
You should see the following on the screen:
[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
Now if you open the output file, hello.pdf, with Adobe Acrobat Reader,
you should see the greeting message, "Hello world!", showing at the
top left corner of the page. This is great. I am starting to like FOP now.
Formatting XML and XSLT Files to a .pdf File
In the previous two examples, the input file is an XSL-FO file which contains
the source information mixed with the XSL formatting objects directly. In this example,
the source information is stored in an normal XML file, and the XSL formatting
objects are mixed with the XSL transformation templates in an XSL file.
So I modified the old hello.xsl into 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>
Note that:
- The formatting objects are embedded as output of the transformation templates.
- There are two name spaces used in this file, "xsl:" and "fo:".
- The transformation template puts two "-" characters around the information of
the root elements in the source XML file.
Let's run FOP with the following command:
run_fop -xml hello_xsl.xml -xsl hello_fo.xsl -pdf hello.pdf
Now if you open the output file, hello.pdf, with Adobe Acrobat Reader,
you should see the greeting message, "-Hello world!-", showing at the
top left corner of the page. This is perfect.
Transforming XML and XSLT Files
The FOP package also contains another XML tool called "xalan". It can be used to
transform an XML file based the transformation templates defined in an XSL file.
To run "xalan", create a batch file, run-xalan.bat, with the following command:
\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
Now let's run "xalan" to transform hello_xsl.xml with hello_fo.xml:
run_xalan -in hello_xsl.xml -xsl hello_fo.xsl -out hello.out
Open hello.out with a text editor, you will see:
<?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>
This is exactly what I expected. The output generated from the transformation
template contains the formatting objects and information from the source XML
file. Note that the format is not well maintained during the transformation.
The first line is too long, I have to truncate it.
So if you have some data organized in an XML file, you can write an XSL file
with transformation templates to re-organize the data. In the same XSL file,
you can also put some formatting objects to format the re-organized data
into a pintable format.
You can finish the transformation and formatting process in two steps:
- 1. Use "xalan" to transform the XML file with the XSL file into a FO file.
- 2. Use "fop" to format the FO file into a PDF file.
Part:
1
2
|