The Default XSLT Rules

XSLT has some built-in, default rules that we've already seen in action. For example, the default rule for text nodes is to add the text in that node to the output document.

The most important default rule applies to elements and can be expressed like this:

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

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

The default rule for text can be expressed like this, where by default, the text of a 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:

 <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 like this:

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

And the same goes for comments, whose default rule can be expressed this way:

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

The upshot of the default rules is that if you don't supply any rules, all the parsed character data in the input document is inserted in the output document. Here's what an 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> 

Here's the result of applying this stylesheet to ch13_01.xml:

 <?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 


Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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