page 32 defs
rect style=fill:urlMyGradient; stroke:black x=1cm y=1cm width=6cm height=2cm
text x=4cm y=2.2cm text-anchor=middle style=font-family:Verdana; font-size:24;
font-weight:bold; fill:black xsl:apply-templates select=greeting
text g
svg xsl:template
xsl:template match=greeting xsl:value-of select=.
xsl:template xsl:stylesheet
As you can see from this stylesheet, most of the code here simply sets up the structure of the SVG document. This is typical of many stylesheets; once you learn what the output format
should be, you merely extract content from the XML source document and insert it into the output document at the correct spot. When we transform the Hello World document with this
stylesheet, here are the results:
?xml version=1.0 encoding=UTF-8? DOCTYPE svg PUBLIC -W3CDTD SVG 20001102EN
http:www.w3.orgTR2000CR-SVG-20001102DTDsvg-20001102.dtd svg height=4cm width=8cm
g defs
radialGradient fy=2cm fx=4cm r=3cm cy=2cm cx=4cm id=MyGradient
stop style=stop-color:red offset=0 stop style=stop-color:blue offset=50
stop style=stop-color:red offset=100 radialGradient
defs rect height=2cm width=6cm y=1cm x=1cm
style=fill:urlMyGradient; stroke:black text style=font-family:Verdana; font-size:24;
font-weight:bold; fill:black text-anchor=middle y=2.2cm x=4cm
Hello, World text
g svg
When rendered in an SVG viewer, our Hello World document looks like Figure 2-2
.
Figure 2-2. SVG version of our Hello World file
This screen capture was made using the Adobe SVG plug-in inside the Internet Explorer browser. You can find the plug-in at
http:www.adobe.comsvg .
page 33
We used Xalan to generate the SVG file:
java org.apache.xalan.xslt.Process -in greeting.xml -xsl svg-greeting.xsl -out greeting.svg
This command should all be on a single line.
2.5.2 The Hello World PDF File
To convert the Hello World file into a PDF file, well first convert our XML file into formatting objects. The Extensible Stylesheet Language for Formatting Objects XSL-FO is
an XML vocabulary that describes how content should be rendered. Here is our stylesheet:
?xml version=1.0? xsl:stylesheet version=1.0
xmlns:xsl=http:www.w3.org1999XSLTransform xmlns:fo=http:www.w3.org1999XSLFormat
xsl:output method=xml xsl:template match=
fo:root xmlns:fo=http:www.w3.org1999XSLFormat fo:layout-master-set
fo:simple-page-master margin-right=75pt margin-left=75pt page-height=11in page-width=8.5in
margin-bottom=25pt margin-top=25pt master-name=main fo:region-before extent=25pt
fo:region-body margin-top=50pt margin-bottom=50pt fo:region-after extent=25pt
fo:simple-page-master fo:page-sequence-master master-name=standard
fo:repeatable-page-master-alternatives fo:conditional-page-master-reference
master-name=main odd-or-even=any fo:repeatable-page-master-alternatives
fo:page-sequence-master fo:layout-master-set
fo:page-sequence master-name=standard fo:flow flow-name=xsl-region-body
xsl:apply-templates select=greeting fo:flow
fo:page-sequence fo:root
xsl:template xsl:template match=greeting
fo:block line-height=40pt font-size=36pt text-align=center xsl:value-of select=.
fo:block xsl:template
xsl:stylesheet
This stylesheet converts our Hello World document into the following XML file:
?xml version=1.0 encoding=UTF-8? fo:root xmlns:fo=http:www.w3.org1999XSLFormat
fo:layout-master-set fo:simple-page-master master-name=main margin-top=25pt
margin-bottom=25pt page-width=8.5in page-height=11in margin-left=75pt margin-right=75pt
fo:region-before extent=25pt fo:region-body margin-bottom=50pt margin-top=50pt
fo:region-after extent=25pt fo:simple-page-master
fo:page-sequence-master master-name=standard fo:repeatable-page-master-alternatives
fo:conditional-page-master-reference odd-or-even=any master-name=main
page 34 fo:repeatable-page-master-alternatives
fo:page-sequence-master fo:layout-master-set
fo:page-sequence master-name=standard fo:flow flow-name=xsl-region-body
fo:block text-align=center font-size=36pt line-height=40pt Hello, World
fo:block fo:flow
fo:page-sequence fo:root
We generated this file of formatting objects with this command:
java org.apache.xalan.xslt.Process -in greeting.xml -xsl fo-greeting.xsl -out greeting.fo
This lengthy set of tags uses formatting objects to describe the size of the page, the margins, font sizes, line heights, etc., along with the text extracted from our XML source document.
Now that we have the formatting objects, we can use the Apache XML Projects FOP tool. After converting the formatting objects to PDF, the PDF file looks like
Figure 2-3 .
Figure 2-3. PDF version of our Hello World file
Heres the command used to convert our file of formatting objects into a PDF file:
java org.apache.fop.apps.CommandLine greeting.fo greeting.pdf
2.5.3 The Hello World Java Program
Our last two transformations dont involve XML vocabularies at all; they use XSLT to convert the Hello World document into other formats. Next, well transform our XML source
document into the source code for a Java program. When the program is compiled and executed, it prints the message from the XML document to the console. Heres our stylesheet:
?xml version=1.0? xsl:stylesheet version=1.0
xmlns:xsl=http:www.w3.org1999XSLTransform xsl:output method=text
xsl:template match= xsl:text
public class Greeting {
public static void mainString[] argv {