2.3. XSLT Stylesheet Terminology

It is often possible to do a great deal of work with a given technology without ever knowing exactly what some of its terms mean. Although XSLT is basically XML, there are very specific uses for its elements and attributes. The basic concepts of stylesheet structure go beyond the basic XML tagging structure to form a logical “programming” application. It is especially important, then, that we detail the terminology of XSLT.

2.3.1. Stylesheet

The term stylesheet is for the most part self-explanatory, but in the context of XSLT, it requires a certain qualification. Literally, an XSLT stylesheet is an XML document instance that is processed by an XSLT engine to perform a transformation on other XML documents. A stylesheet follows XML well-formedness rules, uses XSLT-specific namespace declarations, and can contain one or more XSLT templates that select and process elements from the source XML document.

A common use for XSLT stylesheets is to transform the logical structure of an XML document instance from the semantics and syntax of one DTD or schema to another. This is more of what one might traditionally consider a transformation, as it is more an XSLT logical structure transformation than a specific “styling,” but it is nonetheless an act of styling that is taking place.

2.3.2. Stylesheet Element and Transform Element

The stylesheet element (<xsl:stylesheet>) is a valid XML element that is the first element (i.e., document element) in a stylesheet. It is the parent or ancestor of all the other elements in a stylesheet. The transform element (<xsl:transform>) is allowed as a replacement for the stylesheet element. Only one of either the <xsl:stylesheet> or <xsl:transform> element is allowed in a stylesheet.

Note

<xsl:stylesheet> and <xsl:transform> are synonymous. For convenience in classifying XSLT stylesheets as opposed to XSL Formatting Object stylesheets, you may wish to use <xsl:transform> for all XSLT stylesheets and <xsl:stylesheet> for all XSL stylesheets.


2.3.3. Result Tree

The result tree is simply the output that is produced from the processing of an XSLT stylesheet. Matching the root node of the input XML document instance and generating an output based on the rules found in the stylesheet create this tree. Once the entire stylesheet has been processed, the output—the result tree—is generated. Note that the result tree does not necessarily reflect the structure of the input, or source tree. The transformation process, using the rules in the stylesheet, may restructure the information prior to generating the result tree.

The output, or result tree, from an XSLT stylesheet is always a well-formed XML document, unless another output format like HTML or text is specifically selected. (See Chapter 10 for more information on selecting different kinds of output.)

2.3.4. Source Tree

The source tree is a representation of a well-formed XML document that is created during the parsing of the XML document. The source tree is created by the XSLT processor and stored in “memory” for reference during the processing of the XSLT stylesheet.

A valid source tree will always have a root node. The source tree is the entire XML document instance, but it is a somewhat abstracted roadmap of it, as shown with the book example in Figure 2-1.

Figure 2-1. Creating a source tree.


All nodes from the input XML document, including element nodes, attribute nodes, etc., are included in the source tree, which makes the representation of the structure somewhat unintuitive, as shown in Figure 2-2. This structure was explained in detail in Chapter 1.

Figure 2-2. Logical structure of a source tree.


2.3.5. Whitespace

Whitespace is simply space between characters, whether tabs, line returns, or simply spaces. In most cases, whitespace does not matter to the XML processor unless we tell it to pay attention to it. Sometimes whitespace can be added to make it easier to read a complexly coded XML document.

A good way to think of whitespace is in terms of HTML. An HTML browser automatically collapses (or removes) multiple spaces between words to a single space. Hard returns in elements are also collapsed into a single space, unless the line is tagged with a line break tag (<br>). XML works similarly with whitespace. In HTML, you can force every bit of whitespace to be attended to and rendered by using the preformatted, or <pre>, tag. In XML, you can do this by adding an attribute (xml:space='preserve') to an element or one of its ancestors.

In an XSLT stylesheet, whitespace is handled similarly to how it is handled in XML or HTML. For the most part, it is ignored. Within the quote marks surrounding the value for an attribute, whitespace is “compressed” or “condensed” the way it is in HTML, down to a default of one space. More spaces can be added with the &nbsp;, or nonbreaking space, entity. Space within the output result tree can be added or preserved with an XSLT instruction element, <xsl:text>, discussed in detail in Chapter 6. It serves a role analogous to the HTML <pre></pre> tags (though it does not affect font or style in that way, only preservation of whitespace).

In XSLT stylesheets, whitespace within an attribute value is condensed to a single space. Within and between template rules, whitespace has no effect on XSLT stylesheet processing. Within the <xsl:text> instruction element, whitespace is preserved.

2.3.6. Well-Formedness

Well-formedness is the fundamental syntactic integrity required of every XML document. Whether there is a pre-defined markup specification using a DTD or schema or not, every XML document must be well-formed. The same is true of XSLT stylesheets. All rules apply, from proper matching of lowercase characters to the closing tag required to match every opening tag, etc. In essence, XSLT stylesheets must be well-formed because they are XML document instances.

The proper nesting of elements actually makes understanding each part of the XSLT stylesheet easier, as shown in Example 2-5.

Example 2-5. Basic XSLT template showing nesting of XSLT and literal elements.
<xsl:template match="body">
      <xsl:for-each select="p">
            <i>
                   <xsl:apply-templates />
            </i>
      </xsl:for-each>
</xsl:template>

Each tag that opens also closes, though in one case (<xsl:apply-templates>) the element is empty, so the closing of the tag is signified with the end “/” within the tag. The elements are also nested hierarchically within one another.

Even without knowing the full function of <xsl:for-each>, it is possible to see that something is going to be made italic using the HTML <i> tag. That something—assuming these are standard HTML tags—is a paragraph, selected by the <xsl:for-each> with the select="p" attribute. Not only one paragraph is selected, but each one (for-each) that may be found in the body (matched with match="body" on <xsl:template>) of the XML document instance.

Attribute values in XSLT elements must be contained in quote marks and the quote marks must match—either two single quotes (' ') or two double quotes (“ ”). Both of the following are incorrect:

<season period="spring'>
<season period=summer>

Note

It is good practice to use double quotes for attribute values, as some constructs in XSLT use expressions inside of attribute values, and therefore must use single quotes within the double quotes to avoid confusion and parsing errors.


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

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