Flylib.com

Books Software

 
 
 

Chapter 4: Templates Rule

 
xslt for dummies
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 bricks of a chimney, the shingles of a roof, the pages of this book, nuggets in a 12-piece McNugget Value Meal. Well, you get the idea, right? Template rules are the basic building blocks of XSLT and nothing much happens in transformation-land without them. To use current slang, templates rule!

  
 
 
2000-2002    Feedback
 
xslt for dummies
Chapter 4 - Templates Rule!
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

A Glorified Mail Merge

Template rules can be thought of as a glorified mail merge. Have you ever used the mail merge feature in a word processor? Even if you havent, the concept is pretty simple. To do a mail merge that creates a personalized letter for each customer in a database, for example, you create a letter in a word processor that includes both normal text and data field placeholders. These fields in the word processed document are linked to an external customer database, so that when you print the document, the word processor prints a letter for each customer in the linked database. This mail merge process is very common; in fact, I am willing to bet you have mail sitting in your mailbox right now that was prepared in this manner.

The customer database serves as the source of the information. The word processor document serves as the template. And a printed letter results from the transformation in which the source and template are combined.

Similarly, an XML document acts as the source, while the XSLT stylesheet contains templates. The XSLT processor creates a result document based on the source XML document being applied to the stylesheets template rules. Table 4-1 shows the parallels between the two processes.

Table 4-1: Comparing Mail Merge with XSLT Template Rules

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 certainly a good starting point as you get your hands around template rules.

  
 
 
2000-2002    Feedback
 
xslt for dummies
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 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

start example
<!-- 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>
end example
 

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:

Hey Mr. XSLT Processor, as you examine each of the nodes in the source tree, look for a child of the current node that is named film . If you find a match, please return the node to the template rule so that I can do something with it.

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:

  • Literal text: Plain text that is simply copied to the result tree.

  • XSLT instructions: XSLT instructions that generate text or nodes for the result tree. Common elements you use include xsl:apply-templates , xsl:copy , xsl:copy-of , and xsl:value-of .

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

start example
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
<tv>
<model>1010</model>
<type>WideScreen</type>
<aspectratio>16x9</aspectratio>
</tv>
end example
 

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.

  
 
 
2000-2002    Feedback