3.2. Built-in Template Rules

Template rules use the match attribute to select nodes from the input XML document to be processed. However, XSLT provides a mechanism to handle nodes that are not explicitly addressed, using the built-in template rule. The processor implements built-in template rules automatically.

There are built-in template rules for all node types; however, not all built-in templates generate output. The built-in template for the root node and element nodes simply processes the children of each node, using <xsl:apply-templates> as shown in Example 3-18.

Example 3-18. Built-in template rule for root and element nodes.
<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

The built-in template for text and attribute nodes will send the contents of the node to the output tree as a string of text, and can be represented as shown in Example 3-19. However, most processors will not send the contents of an attribute to the output unless there is an explicit rule to make it happen.

Example 3-19. Built-in template rule for text and attribute nodes.
<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

The built-in template rules for processing-instructions, comments, and namespaces do nothing, effectively ignoring the nodes. The rules for processing-instructions and comments can be represented as an empty element, shown in Example 3-20. However, there is no way to represent a namespace pattern match, so there is no example built-in template rule for namespaces.

Example 3-20. Built-in template rule for processing-instruction and comment nodes.
<xsl:template match="processing-instruction()|comment()"/>

The built-in template rules are treated just like other template rules, however they have a lower priority and import precedence than all other template rules. Explicit template rules always override built-in template rules for the same match pattern.

When a stylesheet has templates that use the mode attribute, as discussed in Section 3.1.3.4, the built-in template rule for modes is invoked. This template rule allows the processing for each mode to continue, even if the node is not explicitly selected by a template rule with that mode. The built-in template rule for modes applies to the root node and any element nodes that are processed with a mode attribute, as shown in Example 3-21.

Example 3-21. Built-in template rule for modes.
<xsl:template match="*|/" mode="modeA">
      <xsl:apply-templates mode="modeA"/>
</xsl:template>

It is important to remember that built-in template rules exist, because the result of processing an XML document is directly affected by these rules when nodes are not explicitly matched. For example, using a very basic stylesheet, as shown in Example 3-22, the built-in template rules apply to all nodes, even though the only explicit match is on the root node.

Example 3-22. Using built-in template rules.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           version="1.0">
<xsl:template match="/">
           <xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

The content of each element is specifically processed by the <xsl:apply-templates> in the built-in template rule for elements, down to the text level, where the text is then passed to the output with the built-in template rule for text.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.62.103