| Previous | Next
The Built-in Template RulesThere are seven kinds of nodes in an XML document: the root node, element nodes, attribute nodes, text nodes, comment nodes, processing instruction nodes, and namespace nodes. XSLT provides a default built-in template rule for each of these seven kinds of nodes that says what to do with that node if the stylesheet author has not provided more specific instructions. These rules use special wildcard XPath expressions to match all nodes of a given type. Together these template rules have major effects on which nodes are activated when. The Default Template Rule for Text and Attribute NodesThe most basic built-in template rule copies the value of text and attribute nodes into the output document. It looks like this: <xsl:template match="text( )|@*"> <xsl:value-of select="."/> </xsl:template> The Example 8-10 is an XSLT stylesheet that pulls the birth and death dates out of the Example 8-10. An XSLT stylesheet that reads attribute<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="people"> <html> <head><title>Famous Scientists</title></head> <body> <dl> <xsl:apply-templates/> </dl> </body> </html> </xsl:template> <xsl:template match="person"> <dt><xsl:apply-templates select="name"/></dt> <dd><ul> <li>Born: <xsl:apply-templates select="@born"/></li> <li>Died: <xsl:apply-templates select="@died"/></li> </ul></dd> </xsl:template> </xsl:stylesheet> When an XSLT processor applies this stylesheet to Example 8-1, it outputs the HTML document shown in Example 8-11. Example 8-11. The HTML document produced by applying Example 8-10 to Example 8-1<html> <head> <title>Famous Scientists</title> </head> <body> <dl> <dt> Alan Turing </dt> <dd> <ul> <li>Born: 1912</li> <li>Died: 1954</li> </ul> </dd> <dt> Richard P Feynman </dt> <dd> <ul> <li>Born: 1918</li> <li>Died: 1988</li> </ul> </dd> </dl> </body> </html> It's important to note that although this template rule says what should happen when an attribute node is reached, by default the XSLT processor never reaches attribute nodes and, therefore, never outputs the value of an attribute. Attribute values are output according to this template only if a specific rule applies templates to them, and none of the default rules do this because attributes are not considered to be children of their parents. In other words, if element The Default Template Rule for Element and Root NodesThe most important template rule is the one that guarantees that children are processed. This is that rule: <xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template> The asterisk Of course, templates may override the default behavior. For example, when you include a template rule matching The Default Template Rule for Comment and Processing Instruction NodesThis is the default template rule for comments and processing instructions: <xsl:template match="processing-instruction()|comment( )"/> It matches all comments and processing instructions. However, it does not output anything into the result tree. That is, unless you provide specific rules matching comments or processing instructions, no part of these items will be copied from the input document to the output document. The Default Template Rule for Namespace NodesA similar template rule matches namespace nodes and instructs the processor not to copy any part of the namespace node to the output. This is truly a built-in rule that must be implemented in the XSLT processor's source code; it can't even be written down in an XSLT stylesheet because there's no such thing as an XPath pattern matching a namespace node. That is, there's no |