Parsing the Transformee How a Stylesheet Is Processed

page 26

2.3.4 Walking Through Our Example

Lets 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. Dont worry too much about what that tree looks like or how it works; for now, just assume that the XSLT processor knows everything thats 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 were 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 xsl:template 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:template s 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 html body h1 xsl:value-of select=. h1 body html xsl:template 6. Now were in the xsl:template for the greeting element. The first three elements in this xsl:template html , body , and h1 are HTML elements. Because theyre 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, were using the XPath expression . a single period to indicate the current node. The XSLT processor looks at the current node the greeting element were currently processing and outputs its text.