XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / A transformation step by step
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
Viewed as tree structures, an XSLT transformation works with document trees rather than documents. The stylesheet is also read by the XML parser and converted into a tree. For the result document, XSLT 1.0 and 2.0 differ: XSLT 1.0 describes a result tree written directly. This top-down approach is relatively easy to visualize.
XSLT 2.0 instead uses a more abstract bottom-up approach, initially treating the result as a stream in the form of a result sequence. Normalization at the start of serialization converts this into a tree too, so applying the same picture here seems reasonably acceptable.
The source document tree is central to an XSLT transformation. Its structure even indirectly controls processing. The tree is traversed from the top, corresponding to the document’s reading order.
Current: The current node in the source document
Processing can be visualized as a cursor moving through the source tree, always positioned on exactly one node, called the current node.
Initially, the current node is automatically the source document node. The XPath pattern representing it is “/.” Its activated template rule is the one with match="/". Processing therefore always starts at the source document node and progressively moves toward its children.
The XSLT processor always starts processing the current node with the matching template rule’s content. This becomes the current template rule. Just as there is only one current node, there is only one current template rule.
With the current node as context, instructions in the current template rule select node sets (sequences) from the source tree using XPath expressions. This set is the current sequence.
Figure: The pattern for the document node.
The sequence’s nodes are then offered to the stylesheet, in order, as current nodes for processing: the current node moves on. Among the available template rules, the processor finds the one whose match pattern fits the offered node. If several rules apply, the most specific match wins and becomes the new current template rule.
The imaginary result tree
As noted, the document-node template rule runs exactly once at the start of processing. If it contains literal result elements, the nodes representing them in the stylesheet tree are copied and appended directly below the newly created result document node.
Results of evaluated instructions and subsequent template rules are handled in the same way, growing the result tree. Compared with imagining source code whose tag containers must be written inward from both ends simultaneously, the tree metaphor is clearly easier to follow.
Once the source has been fully traversed, the transformation ends and the result tree is complete. It can now become an actual document. This flattening of the tree is more commonly called serialization.
The reality in XSLT 2.0
The appealing picture of a growing result tree fits XSLT 1.0 but does not directly apply to XSLT 2.0. There, content is output successively into a result sequence rather than a tree being built section by section. This sequence is serialized after the transformation ends.
Serialization is a separate processing step independent of the transformation. Its sequence normalization either temporarily produces a complete tree or fails entirely. An unfinished tree or document can never result from a transformation, for example through premature termination.
Figure: XSLT 2.0 – many possible sequences.
Unlike XSLT 1.0, XSLT 2.0 can produce multiple result documents, so the processor may create several result sequences. Only one is primary, corresponding to the result document described above. The others are serialized into secondary result documents, created using instruction xsl:result-document.
There are also freely wandering sequences called temporary trees. (The term “tree” is misleading, since these too are only sequences.) Stored in variables, they have their own document nodes. These are not serialized, but can be copied into a result sequence at any chosen time.
This complex picture will come up soon enough. For now, we retain the simple arrangement to introduce XPath path expressions.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn