Formatting an XML Document

Writing an entire document using the XSL formatting objects is not an easy task for anything but short documents. I have a hard time imagining anyone using the formatting objects to write a book, for example. The W3C foresaw that difficulty, and that's one of the main reasons it also introduced the transformation language we took a look at in the previous chapter. You can write a document using your own tags, and you can use XSLT to transform the document so that it uses the XSL formatting objects. In practice, that's almost invariably the way it's done, which means all you have to supply is an XSLT stylesheet that can be used to convert your document to use formatting objects. In this way, an XSLT processor can do all the work for you, transforming a document from a form you're comfortable working with to a formatting object form, which you can then feed to a program that can handle formatting objects and display the formatted result.

To make all this self-evident, I'll write an example here using the XML document we saw in the previous chapter; I'll call this ch14_01.xml here:

Listing ch14_01.xml
 <?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="ch14_02.xsl"?> <PLANETS>   <PLANET COLOR="RED">     <NAME>Mercury</NAME>     <MASS UNITS="(Earth = 1)">.0553</MASS>     <DAY UNITS="days">58.65</DAY>     <RADIUS UNITS="miles">1516</RADIUS>     <DENSITY UNITS="(Earth = 1)">.983</DENSITY>     <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->   </PLANET>   <PLANET COLOR="WHITE">     <NAME>Venus</NAME>     <MASS UNITS="(Earth = 1)">.815</MASS>     <DAY UNITS="days">116.75</DAY>     <RADIUS UNITS="miles">3716</RADIUS>     <DENSITY UNITS="(Earth = 1)">.943</DENSITY>     <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->   </PLANET>   <PLANET COLOR="BLUE">     <NAME>Earth</NAME>     <MASS UNITS="(Earth = 1)">1</MASS>     <DAY UNITS="days">1</DAY>     <RADIUS UNITS="miles">2107</RADIUS>     <DENSITY UNITS="(Earth = 1)">1</DENSITY>     <DISTANCE UNITS="million miles">128.4</DISTANCE><!--At perihelion-->   </PLANET> </PLANETS> 

In this chapter, I'll write an XSLT stylesheet for this document, transforming it so that it uses formatting objects. Then I'll use the fop processor to turn the new document into a PDF file. And I'll take a look at the formatted document with Adobe Acrobat.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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