Top-Level Elements Stylesheet Structure

page 30 Other stuff More obscure elements that can appear as top-level elements are xsl:decimal- format , xsl:namespace-alias , and xsl:attribute-set . All are discussed in Appendix A .

2.4.7 Other Approaches

One mantra of the Perl community is, Theres more than one way to do it. Thats true with XSLT stylesheets, as well. We could have written our stylesheet like this: xsl:stylesheet xmlns:xsl=http:www.w3.org1999XSLTransform version=1.0 xsl:output method=html xsl:template match= html body xsl:apply-templates select=greeting body html xsl:template xsl:template match=greeting h1 xsl:value-of select=. h1 xsl:template xsl:stylesheet In this version, we put the wrapper elements for the HTML document in the template for the root element. One of the things you should think about as you build your stylesheets is where to put elements like html and body . Lets say our XML document looked like this instead: ?xml version=1.0? greetings greetingHello, Worldgreeting greetingHey, Yallgreeting greetings In this case, we would have to put the html and body elements in the xsl:template for the root element. If they were in the xsl:template for the greeting element, the output document would have multiple html elements, something that isnt valid in an HTML document. Our updated stylesheet would look like this: xsl:stylesheet xmlns:xsl=http:www.w3.org1999XSLTransform version=1.0 xsl:output method=html xsl:template match= html body xsl:apply-templates select=greetingsgreeting body html xsl:template xsl:template match=greeting h1 xsl:value-of select=. h1 xsl:template xsl:stylesheet 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