How is a stylesheet laid out? Are there restrictions on element placement? Do they even contain elements? We'll answer these questions here. A stylesheet contains a set of template rules. A template rule has two parts : a pattern, which is matched against nodes in the source tree, and a template, which details a portion of the result tree and how it is constructed (transformed). This enables a stylesheet to be applied to a wide document set that has similar source tree structures. Associating Stylesheets with XML DocumentsOne note of importance here is that the XSLT specification does not designate how an XSLT stylesheet is associated with an XML document. The W3C does recommend, however, that XSL processors follow the method described in the W3C document "Associating Stylesheets with XML Documents Version 1.0" located at http://www.w3.org/1999/06/REC-xml-stylesheet-19990629. This document was made a W3C recommendation on June 29, 1999. In essence, this document states that stylesheets should be associated with an XML document by using a processing instruction whose target is xml-stylesheet . This is similar to the behavior of the HTML 4.0 tag <LINK REL="stylesheet"> . Here's an example: <?xml-stylesheet href="mystyle.css" type="text/css"?> Table 2.5 defines the following attributes for the xml-stylesheet statement. Table 2.5. xml-stylesheet Statement Attributes
To me, the media descriptor is the most interesting. It's rarely used today, but I think you can see there is a lot of potential for the various categories listed. For example, an XSL stylesheet could be utilized to transform an XML document into a Wireless Application Protocol (WAP) document. Those of you who are HTML gurus might have noticed that the linguistics of the attributes are exactly as with <LINK REL="stylesheet"> in HTML 4.0, with the exception of the alternate pseudo attribute. If alternate="yes" is specified, then the processing instruction has the linguistics of <LINK REL="alternate stylesheet"> instead of <LINK REL="stylesheet'> . Let's look at some of the tags and their place in a stylesheet and then move on to a detailed discussion of these things called templates. <xsl:stylesheet>This is the base tag for an XSL stylesheet. It encompasses the entire document. Here is its definition with attributes: <xsl:stylesheet> version = number> <!-- Content: (xsl:import, other top-level-elements) --> </xsl:stylesheet> An <xsl:stylesheet> element must have a version attribute, indicating the version of XSLT that the stylesheet requires. As of this writing, the version of XSLT should be 1.0. I think this value will change very soon to 1.1. This requirement will allow compatibility with future versions of XSLT. Stylesheets can check for the version number and process accordingly . As you know, in any programming language, as versions change so does the language. <xsl:transform> is allowed as a synonym for <xsl:stylesheet> , and it has the identical attributes and definitions as <xsl:stylesheet> . Layout and Order of ElementsThe <xsl:stylesheet> element can contain the following types of elements:
We'll be covering most of these in the rest of this chapter. An element occurring as an immediate child of the <xsl:stylesheet> element is called a top-level element. The order in which these children occur isn't important except for xsl:import elements. If an xsl:import element occurs in a stylesheet, it must be the immediate child of the <xsl:stylesheet> element. The other elements can be ordered in any way you want, and as a result, it isn't necessary for stylesheet creation tools to provide control over the order in which the elements occur. (This should serve to explain the comment tag that's in the example in the preceding section.) Example of Stylesheet StructureListing 2.4 is an example of the structure of a stylesheet. I've left this as a bare-bones example to illustrate element layout. Ellipses ( ) indicate where I've left out attribute values or content. Although this example shows one of each type of allowed element, stylesheets do not have to have all these elements, and there can be more than one of each of them. Listing 2.4 Sample Stylesheet Structure<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="..."/> <xsl:include href="..."/> <xsl:strip-space elements="..."/> <xsl:preserve-space elements="..."/> <xsl:output method="..."/> <xsl:key name="..." match="..." use="..."/> <xsl:decimal-format name="..."/> <xsl:namespace-alias stylesheet-prefix="..." result-prefix="..."/> <xsl:attribute-set name="..."> </xsl:attribute-set> <xsl:variable name="...">...</xsl:variable> <xsl:param name="...">...</xsl:param> <xsl:template match="..."> </xsl:template> <xsl:template name="..."> </xsl:template> </xsl:stylesheet> Again, we'll be discussing most of these elements shortly. |