Default Template Rules

Default Template Rules

Take a look at the following XSLT stylesheet; it has rules that match the root node, <PLANETS> nodes, and <PLANET> nodes:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>      <xsl:template match="PLANETS">          <xsl:apply-templates/>      </xsl:template>      <xsl:template match="PLANET">          <P>              <xsl:value-of select="NAME"/>          </P>      </xsl:template>  </xsl:stylesheet> 

Note the rule for the <PLANETS> element; that rule simply uses <xsl:apply-templates> to apply templates to all its child nodes. However, there is a default rule in template processing that if you dont supply a rule for an element, <apply-templates/> is called automatically. So, the following stylesheet where Ive omitted the rule for <PLANETS is the same as the preceding one:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>      <xsl:template match="PLANET">          <P>              <xsl:value-of select="NAME"/>          </P>      </xsl:template>  </xsl:stylesheet> 

In this case, Ive taken advantage of the default template rules. Here are the default rules for each kind of node, which are put into effect if you dont explicitly give a rule for the node:

  • 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; nothing is copied .

  • Processing instruction nodes. Do no XSLT processing; nothing is copied.

  • Namespace nodes. Do no XSLT processing; nothing is copied.

The most important default rule applies to elements, and can be expressed as follows :

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

This rule is there simply to make sure that every element, from the root on down, is processed with <xsl:apply-templates/> if you dont supply some other rule. If you do supply another rule, it overrides the corresponding default rule.

The default rule for text nodes can be expressed as follows, where the XSLT function text matches the text in the node, so that the text of the text node is added to the output document:

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

The same kind of default rule applies to attributes, which are added to the output document with a default rule like this, where the expression "@*" matches any attribute:

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

By default, processing instructions are not inserted in the output document, so their default rule can be expressed simply as follows using the XSLT function processing-instruction, which matches processing instructions (as youll see in Chapter 8):

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

And the same goes for comments, whose default rule can be expressed as follows using the XSLT function comment , which youll also see in Chapter 8:

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

The upshot of the default rules is that if you dont supply any rules at all, all the parsed character data in the input document is inserted in the output document. Heres what a XSLT stylesheet with no explicit rules looks like:

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

And heres the result of applying this stylesheet to planets.xml. Note that the default rule for attributes has not been applied, because they are not 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 

The Internet Explorer and Default Rules

One of the problems of working with XSLT in the Internet Explorer 5.5 or earlier is that the browser does not supply any default rules. You have to supply all the rules yourself, unless youve installed the MSXML3 processor in replace mode (see Chapter 2 for the details) or youve upgraded to Internet Explorer 6.0.

In addition, whitespace nodes are preserved from the source document, so by default, you can also say this is a template rule: <xsl:output preserve-space ="*"/> .



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

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