Using an XSLT Stylesheet to Transform to XSL-FO

Using an XSLT Stylesheet to Transform to XSL-FO

In this chapter, I create the stylesheet I need to convert planets.xml into planets.fo. I construct this stylesheet step by step; for reference, heres what it is going to look like:

Listing 11.2 planets.xsl
 <?xml version="1.0"?>  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      xmlns:fo="http://www.w3.org/1999/XSL/Format"      version="1.0">      <xsl:template match="PLANETS">          <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">              <fo:layout-master-set>                   <fo:simple-page-master master-name="page"                       page-height="400mm" page-width="300mm"                       margin-top="10mm" margin-bottom="10mm"                       margin-left="20mm" margin-right="20mm">                       <fo:region-body                         margin-top="0mm" margin-bottom="10mm"                         margin-left="0mm" margin-right="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-weight="bold" font-size="36pt"                           line-height="48pt" font-family="Times"                           color="blue">                           The Planets Table                       </fo:block>                       <xsl:apply-templates/>                   </fo:flow>               </fo:page-sequence>          </fo:root>      </xsl:template>      <xsl:template match="PLANET/NAME">          <fo:block font-weight="bold" font-size="28pt"              line-height="48pt" font-family="Times"              font-style="italic">              Planet:              <xsl:apply-templates/>          </fo:block>      </xsl:template>      <xsl:template match="PLANET/MASS">          <fo:block font-size="24pt" line-height="32pt"              font-family="Times">              <fo:inline text-decoration="underline">                  Mass              </fo:inline>:              <xsl:apply-templates/>              [Earth = 1]          </fo:block>      </xsl:template>      <xsl:template match="PLANET/DAY">          <fo:block font-size="24pt" line-height="32pt"              font-family="Times">              <fo:inline text-decoration="underline">                  Day              </fo:inline>:              <xsl:apply-templates/>              [Earth = 1]          </fo:block>      </xsl:template>      <xsl:template match="PLANET/RADIUS">          <fo:block font-size="24pt" line-height="32pt"              font-family="Times">              <fo:inline text-decoration="underline">                  Radius              </fo:inline>:              <xsl:apply-templates/>              miles          </fo:block>      </xsl:template>      <xsl:template match="PLANET/DENSITY">          <fo:block font-size="24pt" line-height="32pt"              font-family="Times">              <fo:inline text-decoration="underline">                  Density              </fo:inline>:              <xsl:apply-templates/>              [Earth = 1]          </fo:block>      </xsl:template>      <xsl:template match="PLANET/DISTANCE">          <fo:block font-size="24pt" line-height="32pt"              font-family="Times">              <fo:inline text-decoration="underline">                  Distance              </fo:inline>:              <xsl:apply-templates/>              million miles          </fo:block>      </xsl:template>  </xsl:stylesheet> 

When you use this stylesheet to transform planets.xsl, you get planets.fo, which uses XSL-FO formatting objects to produce the display you saw in Figure 11.1. Heres what planets.fo looks like:

Listing 11.3 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 color="blue" font-family="Times"                  line-height="48pt" font-size="36pt" font-weight="bold">                  The Planets Table              </fo:block>             <fo:block font-style="italic" font-family="Times"                 line-height="48pt" font-size="28pt" font-weight="bold">                 Planet: Mercury             </fo:block>             <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Mass                  </fo:inline>:                  .0553                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Day                  </fo:inline>:                  58.65                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Radius                  </fo:inline>:                  1516                  miles              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                  Density                  </fo:inline>:                  .983                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Distance                  </fo:inline>:                  43.4                  million miles              </fo:block>              <fo:block font-style="italic" font-family="Times"                  line-height="48pt"                  font-size="28pt" font-weight="bold">                  Planet:                  Venus              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Mass                  </fo:inline>:                  .815                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Day                  </fo:inline>:                  116.75                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Radius                  </fo:inline>:                  3716                  miles              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Density                  </fo:inline>:                  .943                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Distance                  </fo:inline>:                  66.8                  million miles              </fo:block>              <fo:block font-style="italic" font-family="Times"                  line-height="48pt"                  font-size="28pt" font-weight="bold">                  Planet:                  Earth              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Mass                  </fo:inline>:                  1                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Day                  </fo:inline>:                  1                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Radius                  </fo:inline>:                  2107                  miles              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font-size="24pt">                  <fo:inline text-decoration="underline">                      Density                  </fo:inline>:                  1                  [Earth = 1]              </fo:block>              <fo:block font-family="Times" line-height="32pt"                  font- size="24pt">                  <fo:inline text-decoration="underline">                      Distance                  </fo:inline>:                  128.4                  million miles              </fo:block>          </fo:flow>      </fo:page-sequence>  </fo:root> 

To process planets.fo and create a formatted document, Ill use the Apache XML Projects fop. According to Apache:

FOP is the worlds first print formatter driven by XSL formatting objects. It is a Java application that reads a formatting object tree and then turns it into a PDF document. The formatting object tree can be in the form of an XML document (output by an XSLT engine like XT or Xalan) or can be passed in memory as a DOM Document or (in the case of XT) SAX events.

I use fop 0.17, the current version as of this writing (new versions seem to come out just about monthly). You can get fop at http://xml.apache.org/fop for free. The fop download includes the three JAR files youll need: fop.jar, w3c.jar, and xerces.jar, and all of them need to be in the classpath (add the correct paths to these JAR files as needed for your system), as follows :

 C:\>set classpath=.;fop.jar;xerces.jar;w3c.jar 

To convert planets.fo into planets.pdf, you can use the fop class org.apache.fop. apps.CommandLine , passing it the name of the input document, planets.fo, and the output document, planets.pdf, on the command line:

 C:\>java org.apache.fop.apps.CommandLine planets.fo planets.pdf 

And thats it; you can see the final result, planets.pdf, in the Adobe Acrobat reader if you refer back to Figure 11.1.

Now youve seen how the process works; its time to get to the details and see how to create XSL-FO documents. To see this in detail, Im going to take apart Listing 11.2, the XSLT stylesheet that creates planets.fo.

Writing XSL-FO Documents from Scratch

Note that its not actually necessary to create an XSLT stylesheet to transform XSL documents to XSL-FO form. I could have written planets.fo in the form you see in Listing 11.3 from scratchno XSLT stylesheet needed. However, for any but short XML documents, this is not usually a good idea. XSL-FO formatted documents become very long very quickly (compare the length of planets.xml to planets.fo), so you usually do use XSLT stylesheets to create XSL-FO documents (although some examples in the next chapter are short enough to be written using XSL-FO directly).



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net