The Format of a Stylesheet

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.

Code Listing 5.1. catalog-simple.xsl—A Simple Stylesheet
 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]&#xD;&#xA;</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-space

The 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.

Table 5.1. XSL Top-Level Elements
Element Description
xsl:import Import another stylesheet into the current one. Rules from the stylesheet to be imported are always of lower precedence than the rules specified in the parent stylesheet.
xsl:include Include another stylesheet into the current one. Performs exactly as if the import statement was replaced character for character with the included stylesheet.
xsl:strip-space Defines whether whitespace is stripped and for what elements based on the elements= attribute. Top level may be specified 0 to n times.
xsl:preserve-space Defines whether whitespace is preserved and for what elements based on the elements= attribute. Top level may be specified 0 to n times.
xsl:output Defines the output method. Supported values for the method= attribute are html, text, xml (default). Top level may be specified.
xsl:key Defines a way to work with documents that contain an implicit reference structure. Keys can be used to generate tables of hyperlinks and so on.
xsl:locale Defines a locale that controls how certain number formatting is applied (decimal, comma usage, percent, trailing zeros, and so on).
xsl:attribute-set Allows for the definition of attribute sets, which are named groups of attributes that can later be used as a whole.
xsl:variable Allows the developer to specify variables that can be used within a stylesheet either directly or within expressions.
xsl:param Similar to xsl:variable. Can be used to pass variables to named templates.
xsl:template Defines the template construction rules for translating one XML document to another.

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.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.58.122