Other Approaches Stylesheet Structure

page 31 Notice that we had to modify our XPath expression; what was originally greeting is now greetingsgreeting . As we develop stylesheets, well have to make sure our XPath expressions match the document structure. When you get unexpected results, or no results, an incorrect XPath expression is usually the cause. As a final example, we could also write our stylesheet with only one xsl:template : xsl:stylesheet xmlns:xsl=http:www.w3.org1999XSLTransform version=1.0 xsl:output method=html xsl:template match= html body h1 xsl:value-of select=greeting h1 body html xsl:template xsl:stylesheet Although this is the shortest of our sample stylesheets, our examples will tend to feature a number of short templates, each of which defines a simple transform for a few elements. This approach makes your stylesheets much easier to understand, maintain, and reuse. The more transformations you cram into each xsl:template , the more difficult it is to debug your stylesheets, and the more difficult it is to reuse the templates elsewhere.

2.5 Sample Gallery

Before we get into more advanced topics, well transform our Hello World document in other ways. Well look through simple stylesheets that convert our small XML document into the following things: • A Scalable Vector Graphics SVG File • A PDF file • A Java program • A Virtual Reality Modeling Language VRML file

2.5.1 The Hello World SVG File

Our first example will convert our Hello World document into an SVG file: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xsl:output method=xml doctype-public=-W3CDTD SVG 20001102EN doctype-system= http:www.w3.orgTR2000CR-SVG-20001102DTDsvg-20001102.dtd xsl:template match= svg width=8cm height=4cm g defs radialGradient id=MyGradient cx=4cm cy=2cm r=3cm fx=4cm fy=2cm stop offset=0 style=stop-color:red stop offset=50 style=stop-color:blue stop offset=100 style=stop-color:red radialGradient 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 .