Parsing the Stylesheet How a Stylesheet Is Processed
2.3.2 Parsing the Transformee
Now that the XSLT processor has processed the stylesheet, it needs to read the document its supposed to transform. The XSLT processor builds a tree view from the XML source. This tree view is what well keep in mind when we build our stylesheets.2.3.3 Lather, Rinse, Repeat
Finally, were 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. Well talk about the context extensively in the next chapter. Note: all XSLT processors enjoy being anthropomorphized, so Ill often refer to them this way. While any nodes are in the context, do the following: • 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—its the one that begins xsl:template match= . • 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=htmlbodyh1p is more specific than xsl:template match=p . See the discussion of the xsl:template element for more information. If no xsl:template s match, the XSLT processor uses some built-in rules. See Section 2.4.5 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 xsl:template may in turn invoke other xsl:template s, which invoke xsl:template s as well. This model takes some getting used to, but it is actually quite elegant once youre accustomed to it. 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 youve written, everything starts in main . Similarly, no matter how many xsl:template s youve defined in your stylesheet, everything starts in xsl:template match= . page 262.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.Parts
» O'Reilly-XSLT-Mastering.XML.Transformati... 2264KB Mar 29 2010 05:03:43 AM
» An XML document must be contained in a single element
» XML declarations Document Type Definitions DTDs and XML Schemas
» Well-formed versus valid documents
» Tags versus elements XML Document Rules
» Namespaces XML Document Rules
» The Extensible Stylesheet Language XSL
» Document Object Model DOM Level 1
» Document Object Model DOM Level 2
» Namespaces in XML XML Standards
» Associating stylesheets with XML documents
» Installing Xalan Getting Started
» Our Sample Document A Sample Stylesheet
» Transforming the XML Document
» Stylesheet Results Transforming Hello World
» Parsing the Stylesheet How a Stylesheet Is Processed
» Parsing the Transformee How a Stylesheet Is Processed
» Lather, Rinse, Repeat How a Stylesheet Is Processed
» The xsl:template for greeting Elements
» Built-in template rule for element and root nodes
» Built-in template rule for modes
» Built-in template rule for text and attribute nodes
» Top-Level Elements Stylesheet Structure
» Other Approaches Stylesheet Structure
» The Hello World Java Program
» Goals of This Chapter Summary
» The Root Node The XPath Data Model
» Element Nodes The XPath Data Model
» Attribute Nodes The XPath Data Model
» Text Nodes The XPath Data Model
» Comment Nodes The XPath Data Model
» Processing Instruction Nodes The XPath Data Model
» Namespace Nodes The XPath Data Model
» Simple Location Paths Location Paths
» Relative and Absolute Expressions
» Selecting attributes Selecting Things Besides Elements with Location Paths
» Selecting the text of an element
» Selecting comments, processing instructions, and namespace nodes
» Using Wildcards Location Paths
» Numbers in predicates Predicates
» Functions in predicates Predicates
» Attribute Value Templates XPath Datatypes
» Output View The XPath View of an XML Document
» The Stylesheet The XPath View of an XML Document
» Summary XPath: A Syntax for Describing Needles and Haystacks
» Converting to boolean values
» Boolean examples The xsl:if Element
» xsl:for-each example The xsl:for-each Element
» How It Works Invoking Templates by Name
» Templates à la Mode Invoking Templates by Name
» Defining a Parameter in a Template
» Microsofts XSLT tools Global Parameters
» Setting global parameters in a Java program
» Are These Things Really Variables?
» Procedural design Implementing a String Replace Function
» Recursive design Implementing a String Replace Function
» Template Design Implementation A Stylesheet That Emulates a for Loop
» The Complete Example A Stylesheet That Emulates a for Loop
» XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Template Design A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Complications A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Summary Branching and Control Elements
» The ID, IDREF, and IDREFs Datatypes
» An XML Document in Need of Links
» A Stylesheet That Uses the id Function
» Limitations of IDs Generating Links with the id Function
» Defining a key Generating Links with the key Function
» A Slightly More Complicated XML Document in Need of Links
» The key function and the IDREFS datatype
» Solution 1: Replace the IDREFS datatype
» Solution 2: Use the XPath contains function
» Solution 3: Use recursion to process the IDREFS datatype
» Solution 4: Use an extension function
» Advantages of the key Function
» An Unstructured XML Document in Need of Links
» The generate-id Function Generating Links in Unstructured Documents
» Summary Creating Links and Cross-References
» Our First Example Sorting Data with xsl:sort
» Whats the deal with that syntax?
» Attributes The Details on the xsl:sort Element
» Another Example Sorting Data with xsl:sort
» Our First Attempt Grouping Nodes
» A Brute-Force Approach Grouping with xsl:variable
» Summary Sorting and Grouping Elements
» Recursive design An Aside: Doing Math with Recursion
» Generating output to initialize a variable
» Overview Invoking the document Function
» The document Function and Sorting
» Implementing Lookup Tables More Sophisticated Techniques
» Grouping Across Multiple Documents
» Summary Combining XML Documents
» Example: Generating multiple output files
» Example: Using extension functions from multiple processors
» Example: A library of trigonometric functions
» Example: Writing extensions in other languages
» Fallback Processing Extension Elements, Extension Functions, and Fallback Processing
» Extending the Saxon Processor
» Generating JPEG Files from XML Content
» About the Toot-O-Matic Case Study: The Toot-O-Matic
» Make It Easier to Create Tutorials
» Individual Panels Tutorial Layout
» Email Panel Zip File PDF Files
» Individual Panels XML Document Design
» Stylesheets and Modes XSLT Source Code
» Initializing Global Variables XSLT Source Code
» Generating the Main Menu Panel
» Generating the Section Indexes
» Generating the Individual Panels
Show more