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:

  1. Do I have any nodes to process? The nodes to process are represented by 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:

  1. Get the next node from the context. Do I have any <xsl:template>s that match it? (In our example, the next node is the root node, represented in XPath syntax by /.) There is a template that matches this node—it’s the one that begins <xsl:template match="/">.

  2. If one or more <xsl:template>s match, pick the right one and process it. The right one is the most specific template. For example, <xsl:template match="/html/body/h1/p"> is more specific than <xsl:template match="p">. (See the discussion of <xsl:template> in Appendix A for more information.) If no <xsl:template>s match, the XSLT processor uses some built-in rules. See the section Built-in Template Rules” later in this chapter for more information.

Notice that this is a recursive processing model. We process the current node by finding the right xsl:template for it. That ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.