|
||||||||
| Chapter 4 - Templates Rule! | |
| XSLT For Dummies | |
| by Richard Wagner | |
| Hungry Minds 2002 | |
Chapter 4: Templates Rule!
In This Chapter
Template rules are the foundation of XSLT. Not only do they specify what information should be pulled from the source document, but template rules also define what the resulting document is going to look like. Think of them as the
|
|||||||||||
|
||||||||
| Chapter 4 - Templates Rule! | |
| XSLT For Dummies | |
| by Richard Wagner | |
| Hungry Minds 2002 | |
A
|
|||||||||||||||||||||||
|
Item |
Mail Merge |
XSLT |
|---|---|---|
|
Data Source |
External database |
XML document |
|
Template |
Word processor document with linked fields |
XSLT stylesheet with template rules |
|
Result |
Printed letter with merged information |
Result document, such as a web page |
Some may argue that it is an oversimplification to compare XSLT transformations with a mail merge, but even if it is, the comparison isnt far off and is
|
|
||
|
|
|
| 2000-2002 Feedback | |
|
||||||||
| Chapter 4 - Templates Rule! | |
| XSLT For Dummies | |
| by Richard Wagner | |
| Hungry Minds 2002 | |
Basics of a Template Rule
A
template rule
transforms a set of XML nodes you specify from the source document into a new look. A cookie
<xsl:template match=""> </xsl:template>
As shown in Figure 4-1, each template rule consists of two key
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
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
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
<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
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
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 processedWhen 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
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
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.
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.
|
|||||||||||||||