- Chapter 1: Essential XSLT
- A Little Background
- XML Documents
- What Does XML Look Like in a Browser?
- XSLT Transformations
- Making an XSLT Transformation Happen
- Using Standalone XSLT Processors
- Using Browsers to Transform XML Documents
- Using XSLT and JavaScript in the Internet Explorer
- XSLT Transformations on Web Servers
- XML-to-XHTML Transformations
- XSLT Resources
- XSL Formatting Objects: XSL-FO
- XSL-FO Resources
- Formatting an XML Document
- The XSLT Stylesheet
- Transforming a Document into FormattingObject Form
- Creating a Formatted Document
Transforming a Document into FormattingObject Form
To transform planets.xml into a document that uses formatting objects, which I'll call planets.fo, all I have to do is apply the stylesheet planetsPDF.xsl. You can do that using the XSLT techniques you already saw in this chapter.
For example, to use Xalan to create planets.fo, you first set the classpath something like this in Windows:
C:\>set classpath=c:\xalan\xalan-j_2_0_0\bin\xalan.jar; c:\xalan\xalan-j_2_0_0\bin\xerces.jar
Then you apply planetsPDF.xsl to planets.xml to produce planets.fo:
C:\planets>java org.apache.xalan.xslt.Process _-IN planets.xml -XSL planetsPDF.xsl -OUT planets.fo
The document planets.fo uses the XSL formatting objects to specify how the document should be formatted. Here's what planets.fo looks like:
Listing 1.9 planets.fo
<?xml version="1.0" encoding="UTF-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master margin-right="20mm" margin-left="20mm" margin-bottom="10mm" margin-top="10mm" page-width="300mm" page-height="400mm" master-name="page"> <fo:region-body margin-right="0mm" margin-left="0mm" margin-bottom="10mm" margin-top="0mm"/> <fo:region-after extent="10mm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-name="page"> <fo:flow flow-name="xsl-region-body"> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt" font-weight="bold"> Name: Mercury </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Mass (Earth = 1): .0553 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Day (Earth = 1): 58.65 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Radius (in miles): 1516 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Density (Earth = 1): .983 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Distance (million miles): 43.4 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt" font-weight="bold"> Name: Venus </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Mass (Earth = 1): .815 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Day (Earth = 1): 116.75 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Radius (in miles): 3716 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Density (Earth = 1): .943 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Distance (million miles): 66.8 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt" font-weight="bold"> Name: Earth </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Mass (Earth = 1): 1 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Day (Earth = 1): 1</fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Radius (in miles): 2107 </fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Density (Earth = 1): 1</fo:block> <fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Distance (million miles): 128.4 </fo:block> </fo:flow> </fo:page-sequence> </fo:root>
OK, now we've created planets.fo. How can we use it to create a formatted PDF file?