Understanding the Default Template Rules

XSLT has default rules for each kind of node, which are put into effect if you don't explicitly give a rule for the node. Here are those rules:

  • Root node Call <xsl:apply-templates/> by default.

  • Element nodes Call <xsl:apply-templates/> by default.

  • Attribute nodes Copy the attribute value to the result document. But copy it as text, not as an attribute.

  • Text nodes Copy the text to the result document.

  • Comment nodes Do no XSLT processing, which means that nothing is copied .

  • Processing instruction nodes Do no XSLT processing, which means that nothing is copied.

  • Namespace nodes Do no XSLT processing, which means that nothing is copied.

Of these, the most important default rule applies to elements, and can be expressed like this:

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

As you can see, this rule simply makes sure that every element is processed with <xsl:apply-templates/> if you don't supply some other rule. It's important to realize that if you do supply another rule, that new rule overrides the corresponding default rule.

The default rule for text nodes can be expressed like this, where the text of the text node is added to the output document:

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

In addition, the same kind of default rule applies to attributes, which are added to the output document with a default rule like this:

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

In addition, by default, processing instructions are not inserted in the output document. That means their default rule can be expressed simply like this:

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

Comments aren't copied over by default either, so their default rule can be expressed this way:

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

Now that you know these default rules, it shouldn't surprise you that if you don't supply any rules at all, all the parsed character data (PCDATA in XML terms) in the input document is inserted in the output document by default.

For example, here's what an XSLT stylesheet with no explicit rules looks like:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> </xsl:stylesheet> 

Here's what you get when you apply this stylesheet to ch05_01.xml (note that the default rule for attributes has not been applied, because they are not considered children of other nodes):

 <?xml version="1.0" encoding="UTF-8"?>     Mercury     .0553     58.65     1516     .983     43.4     Venus     .815     116.75     3716     .943     66.8     Earth     1     1     2107     1     128.4 

Knowing the default rules is important; if you see this kind of result from your XSLT stylesheets and you're not expecting it, you now know what's causing the problemthe default rules are being applied. If you don't want text stripped from the source document and inserted into the output document in this way, supply an empty rule (one that just matches a node or node-set but doesn't do anything else) or rules to override the applicable default rule or rules. If a rule for a node is empty, the content of the matched node will not be copied to the output document. In this way, you can remove content from the source document when you write the output document.


Internet Explorer does not supply any default XSLT rules. You have to supply all the rules yourself that you want to use in templates.

XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

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