Built-In Template Rules

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

As you begin to work with template rules, you need to know some behind-the-scenes activities I informally call lazy mans template rules because you dont have do anything in order to have them run. These actions are actually called built-in template rules, and the XSLT processor uses them to process any node that isnt matched with an explicitly defined template rule in your stylesheet. Different built-in template rules are applied to each node type.

 Remember   The template rules you create in your stylesheet override built-in template rules.

Element nodes

For element nodes, I demonstrate the built-in template rule by using the ever-popular miniscore.xml file (refer to Listing 4-3) as the source document. Suppose you transform it by using an empty stylesheet:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> </xsl:stylesheet> 

The output is:

 <?xml version="1.0" encoding="utf-8"?> A Little Princess Patrick Doyle 1995 100 

Now, this output looks eerily familiar to the first example using xsl: apply-templates . That is no accident , because the built-in template that is run on element nodes and the root node looks like the following:

 <xsl:template match="*/"> <xsl:apply-templates/> </xsl:template> 

The match pattern is a global catchall to process all nodes (child nodes of the current node and the root node), and xsl:apply-templates transforms each element and sheds the tags.

Text and attribute nodes

For text and attribute nodes, a built-in template rule copies their text through into the result tree:

 <xsl:template match="text()@*"> <xsl:value-of select="."/> </xsl:template> 

In this rule, the match pattern returns all text nodes with text() and all attribute nodes with @* . The xsl:value-of instruction then transforms the current node to the result tree as text.

 Tip   If you wanted to strip out all text nodes, use the following template rule to override the built-in template rule for text nodes:

 <xsl:template match="text()"/> 

Processing instructions, comments, and namespaces

A built-in template rule strips processing instructions and comments so that neither is carried over to the result tree:

 <xsl:template match="processing-instruction()comment()"/> 

The match pattern returns every processing instruction and comment nodes; because the template is empty, nothing is added to the result tree.

Namespace nodes are also removed during processing. An XPath expression for matching a namespace node doesnt exist, so you cant express namespaces as a built-in template or override this built-in rule with your own.

 Tip   You also have the option of not defining a template for a template rule. If you did so, the template rule looks like this:

 <xsl:template match="film"></xsl:template> 

or

 <xsl:template match="film"/> 

When this template rule is triggered, there is no template to output, so it outputs nothing.

When a template rule doesnt contain a template, the rule removes the match pattern from the output.

  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net