Listing 5.1 shows a simple XSL stylesheet. Let's examine it and break it down line by line before moving on to examine each of the features of XSL.
1: <?xml version="1.0"?> 2: <!-- 3: A simple style sheet for placing a header and footer on any document. 4: Al Saganich for Macmillan USA 5: --> 6: 7: <!-- Use the transform version 1.0 namespace -- > 8:<xsl:stylesheet 9:xmlns:xsl="http://www.w3.org/XSL/Transform/1.0" 10: indent-result="no" default-space="strip"> 11:<!-- <xsl:output method="text"> --> 12: <!-- 13: Format for transforms are match rule followed by action. 14: This transform matches the root element 15: Prints [header] followed by a cr-lf pair 16: 17: Then the contents of the document 18: 19: followed by [trailer] 20: --> 21: <xsl:template match="/"> 22: <xsl:text>[heading]
</xsl:text> 23: <xsl:apply-templates /> 24: <xsl:text>[trailer]</xsl:text> 25: </xsl:template> 26: </xsl:stylesheet> 27: |
Examining Listing 5.1, we see the standard XML prolog as well as a number of comments. The first lines of note are actually lines 8 and 9, which state that this is, in fact, an XSL stylesheet and is specified using the XSL namespace (refer to the section, "The FO Namespace," in this chapter). Remember that the XSL namespace is never accessed as a real URI and may in fact not point to anything useful at all. Namespaces are simply a method of specifying the input and/or output of the XSL transformations. An XSL transform will never attempt to access the URI's namepaces. Line 11, even though it's commented out, specifies the output method for this stylesheet, in this case text. Lines 12–20 describe the expected result of applying the stylesheet to an appropriate document. Lines 21 through 26 are the real meat of the stylesheet and bear more serious review.
xsl:output and xsl:strip-spaceThe most recent version of the XSLT specification replaces the result-ns, result-version, result-encoding, and indent-result attributes of the xsl:stylsheet with xsl:output and xsl:strip-space. However, the version of XT shipped with the CD-ROM only supports these attributes, so all examples use the older mechanism. Many of the examples in Chapter 7, "Swing and XML," contain the newer code, albeit commented out. |
XSL is actually specified via XML. Appendix A, "XML Tools," contains the complete non-normative description of XSL. As such, XSL is normally thought of in general terms as XML in that it has elements, nodes, attributes, and so on. The xsl:stylesheet element, which is the root of the XSL Stylesheet XML tree, can have eleven child elements. We will examine each in turn. Table 5.1 briefly describes each and its purpose.
Caution
Note that with the exception of xsl:import, the order in which top-level attributes are specified is arbitrary. xsl:import must precede all other top-level XSL elements or an error will occur.
18.224.58.122