Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The simplest situation is where it finds just one rule that matches this element, for example one declared as:
If it finds more than one matching template rule, it has to use its conflict resolution policy to choose the best fit. The other possibility is that there is no matching template rule: in this case it invokes the built-in template rule for element nodes, which simply executes
: in other words, it selects the children of this element, and tries to find template rules that match these children. There's also a built-in template rule for text nodes, which copies the text node to the output. If the element has no children,
does nothing.
Whatever happens, however, the result of evaluating the
instruction, and are used to form the children (and potentially also the attributes) of the new
element. The new
and
elements now form a sequence that's used to make the children of the
element; and because this template rule was the first one to be activated, the transformation is now complete and the tree with this
element at the top becomes the final result tree of the transformation. (The
element is automatically wrapped in a document node, to complete the process.)
Hopefully, you never actually need to analyze what's going on to this level of detail. The name
template
was chosen because you can think of the whole process as producing a simple fill-in-the-blanks copy of the elements in the stylesheet as elements in the result tree. In the case of literal result elements and literal text, they are copied across unchanged; in the case of XSLT instructions, some processing is performed to fetch data from a source document for insertion at this point in the result tree.
Push Processing
The simplest way to process a source tree is thus to write a template rule for each kind of node that can be encountered, and for that template rule to produce any output required, as well as to call
Example: Push Processing
This example demonstrates the push processing technique: a rule-based stylesheet in which there is one template rule to process each different kind of node.
Input
The source document,
books.xml
, is a simple book catalog:
Stylesheet
Say you want to display this data in the form of a sequentially numbered booklist. The following stylesheet,
books.xsl
, will do the trick:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
A list of books
What's happening here? There's no template rule for the document node, so the built-in template gets invoked. This processes all the children of the document node.
There's only one child of the document node, the The children of the This template makes no further call on Output A list of books etc..
to cause its own children to be processed. These children are all
match=“book”
. This template rule outputs an HTML
element, and within it a
element, which it fills by executing the
instruction whose effect is to get the sequence number of the current node (the
once again to process the children of the
match=“author | title | price”
(you can read
|
as “or”). This template rule outputs an HTML
element that it fills by executing an instruction
.
, which returns the string value of the current node, that is the textual content of the current1 Nigel Rees Sayings of the Century 8.95 2 Evelyn Waugh Sword of Honour 12.99 Other books