home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeXSLSearch this book

2.3. How a Stylesheet Is Processed

Now that we're giddy with the excitement of having transformed an XML document, let's discuss the stylesheet and how it works. A big part of the XSLT learning curve is figuring out how stylesheets are processed. To make this clear, we'll go through the steps taken by the stylesheet processor to create the HTML document we want.

2.3.2. Parsing the Transformee

Now that the XSLT processor has processed the stylesheet, it needs to read the document it's supposed to transform. The XSLT processor builds a tree view from the XML source. This tree view is what we'll keep in mind when we build our stylesheets.

2.3.3. Lather, Rinse, Repeat

Finally, we're ready to begin the actual work of transforming the XML document. The XSLT processor may set some properties based on your stylesheet (in the previous example, it would set its output method to HTML), then it begins processing as follows:

  • Do I have any nodes to process? The nodes to process are represented by something called the context. Initially the context is the root of the XML document, but it changes throughout the stylesheet. We'll talk about the context extensively in the next chapter. (Note: all XSLT processors enjoy being anthropomorphized, so I'll often refer to them this way.)

While any nodes are in the context, do the following:

Notice that this is a recursive processing model. We process the current node by finding the right xsl:template for it. That xsl:template may in turn invoke other xsl:templates, which invoke xsl:templates as well. This model takes some getting used to, but it is actually quite elegant once you're accustomed to it.

NOTE: If it helps, you can think of the root template (<xsl:template match="/">) as the main method in a C, C++, or Java program. No matter how much code you've written, everything starts in main. Similarly, no matter how many <xsl:template>s you've defined in your stylesheet, everything starts in <xsl:template match="/">.

2.3.4. Walking Through Our Example

Let's revisit our example and see how the XSLT processor transforms our document:

  1. The XSLT stylesheet is parsed and converted into a tree structure.

  2. The XML document is also parsed and converted into a tree structure. (Don't worry too much about what that tree looks like or how it works; for now, just assume that the XSLT processor knows everything that's in the XML document and the XSLT stylesheet. After the first two steps are done, when we describe various things using XSLT and XPath, the processor knows what we're talking about.)

  3. The XSLT processor is now at the root of the XML document. This is the original context.

  4. There is an xsl:template that matches the document root:

    <xsl:template match="/">
      <xsl:apply-templates select="greeting"/>

    A single forward slash (/) is an XPath expression that means "the root of the document."

  5. Now the process begins again inside the xsl:template. Our only instruction here is to apply whatever xsl:templates might apply to any greeting elements in the current context. The current context inside this template is defined by the match attribute of the xsl:template element. This means the XSLT processor is looking for any greeting elements at the document root.

    Because one greeting element is at the document root, the XSLT processor must deal with it. (If more than one element matches in the current context, the XSLT processor deals with each one in the order in which they appear in the document; this is known as document order.) Looking at the greeting element, the xsl:template that applies to it is the second xsl:template in our stylesheet:

    <xsl:template match="greeting">
            <xsl:value-of select="."/>
  6. Now we're in the xsl:template for the greeting element. The first three elements in this xsl:template (<html>, <body>, and <h1>) are HTML elements. Because they're not defined with a namespace declaration, the XSLT processor passes those HTML elements through to the output stream unaltered.

    The middle of our xsl:template is an xsl:value-of element. This element writes the value of something to the output stream. In this case, we're using the XPath expression . (a single period) to indicate the current node. The XSLT processor looks at the current node (the greeting element we're currently processing) and outputs its text.

    Because our stylesheet is an XML document (we're really harping on that, aren't we?), we have to end the <h1>, <body>, and <html> elements here. At this point, we're done with this template, so control returns to the template that invoked us.

  7. Now we're back in the template for the root element. We've processed all the <greeting> elements, so we're finished with this template.

  8. No more elements are in the current context (there is only one root element), so the XSLT processor is done.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.