|Chapter 4 - Templates Rule!|
|XSLT For Dummies|
|by Richard Wagner|
|Hungry Minds 2002|
A template rule transforms a set of XML nodes you specify from the source document into a new look. A cookie cutter template inside the template rule contains all the specifications for what this new look should be like. You create a temple rule using an xsl:template element with basic boilerplate code that looks like this:
<xsl:template match=""> </xsl:template>
As shown in Figure 4-1, each template rule consists of two key parts : the match pattern and the template.
Figure 4-1: Parts of a template rule.
Pulling with match patterns
The match pattern is an XPath expression that pulls the nodes that you want to transform from the source tree. Think of a match pattern as something like the list your parents gave you to go to the supermarket for eggs, milk, and cheese. When you entered the store, you would scurry up and down all of the aisles searching for the specific groceries on the list. When you found an item, youd drop it into the basket and continue until everything was checked off of the list. (And if you were like me, you probably threw in a candy bar for good measure in the checkout aisle.)
In the same way, a match pattern defines a list of nodes that you want to be included in the result document (also known as a result tree ). It does so by specifying the conditions you want a node to meet in order for it to be included. The template rule uses the match pattern and scurries through the source document looking for nodes that match these conditions. (However, Ive yet to see a template rule throw in a candy bar into the result document!)
To be used by the template rule, the match pattern is placed as the value of the match attribute and takes the form of a specific XPath expression called a location path .
Tip XML source and result documents are treated as trees by XSLT because they have a hierarchical tree-like structure to them. Therefore, for most purposes, the terms source document and source tree are interchangeable, as are result document and result tree . For more information on trees, see Chapter 3.
You can find out about XPath location paths in Chapter 5, but I can explain a bit now to help you get the gist of what they do for template rules. Consider the XML file in Listing 4-1.
Listing 4-1: score.xml
<!-- score.xml --> <scores> <score id="1"> <film>A Little Princess</film> <composer>Patrick Doyle</composer> <year>1995</year> <grade>100</grade> </score> <score id="2"> <film>Chocolat</film> <composer>Rachel Portman</composer> <year>2001</year> <grade>90</grade> </score> <score id="3"> <film>Vertigo</film> <composer>Bernard Herrmann</composer> <year>1956</year> <grade>95</grade> </score> <score id="4"> <film>Field of Dreams</film> <composer>James Horner</composer> <year>1989</year> <grade>96</grade> </score> <score id="5"> <film>Dead Again</film> <composer>Patrick Doyle</composer> <year>1991</year> <grade>97</grade> </score> </scores>
From this source document, suppose you want to get each of the film elements and do something with them. To do so, set your match element to be the name of the element:
<xsl:template match="film"> <!-- Do something with the film elements --> </xsl:template>
In plain English, this match pattern says:
Each of the five film elements is processed by the template rule.
Remember XPath location paths can get pretty uh shall I say interesting. (Translation: They can look like gobbledygook!) Dont concern yourself too much with XPath location paths now. I save all that fun for Chapter 5.
Pushing with templates
Because of their similarity, the terms template and template rule are often confused and tend to be sloppily interchanged. But there is a fundamental difference in meaning: A template rule is the entire xsl:template element, and the template is everything inside the start and end tags of xsl:template . The purpose of the template is to define how the returning node set is pushed (or output) to the result tree.
A template contains two types of information:
In the following example, The film: is normal text, and the xsl:value-of instruction in an XSLT element that is evaluated at processing time to generate text:
<xsl:template match="score"> The film: <xsl:value-of select="film"/> </xsl:template>
How a template rule is processed
When a template rule is processed, the XSLT processor reads through an incoming XML document and assembles it as a tree. After this tree structure is defined, the processor starts walking through each node of the tree, looking for the most specific matching template rule for each node it encounters. When it finds a match, the processor uses the selected template as its guideline for how to add the node set to the result tree.
To illustrate , suppose you have the XML snippet shown in Listing 4-2.
Listing 4-2: tv.xml
<tv> <model>1010</model> <type>WideScreen</type> <aspectratio>16x9</aspectratio> </tv>
And want to output it :
Model 1010 has an aspect ratio of 16x9.
The template rule that can do this transformation looks like:
<xsl:template match="tv"> Model <xsl:value-of select="model"/> has an aspect ratio of <xsl:value-of select="aspectratio"/>. </xsl:template>
When the XSLT processor performs this transformation, it first constructs a source tree like the one shown in Figure 4-2. Next, as it gets to each node, it checks to see if the node matches the lone template rule Ive defined. The processor scores a bulls-eye when it gets to the root node, because the root node has a tv element node as its child.
Figure 4-2: Source tree.
The processor then gets the template rules template and combines the literal text with the results of the two xsl:value-of elements (see Figure 4-3). These are combined into a single text node and added to the result trees root node, as shown in Figure 4-4.
Figure 4-3: Text nodes of the template.
Figure 4-4: Result tree.
Tip When working with namespaces in your stylesheets, a good rule is that elements in a stylesheet in the xsl: namespace are part of the XSLT language, while non-xsl: elements within a template rule are literal elements put into the result tree.