10.5 Built-in Template Rules

XSLT processors have a feature known as built-in template rules. The built-in template rules were discussed in various places earlier in the book. Because of the built-in templates, an XSLT processor will process nodes in a source document, even though there are no explicit, matching template rules present in the stylesheet used to process the source document.

To illustrate how built-in templates work, here is a ridiculously simple example. The source document mammals.xml, shown in Example 10-14, lists a few mammals that are native to North America.

Example 10-14. A list of some native North American mammals
<?xml version="1.0"?>     <mammals locale="North America">  <mammal>American Bison</mammal>  <mammal>American black bear</mammal>  <mammal>Bighorn sheep</mammal>  <mammal>Bobcat</mammal>  <mammal>Common gray fox</mammal>  <mammal>Cougar</mammal>  <mammal>Coyote</mammal>  <mammal>Gray wolf</mammal>  <mammal>Mule deer</mammal>  <mammal>Pronghorn</mammal>  <mammal>White-tailed deer</mammal> </mammals>

The rather boring stylesheet blank.xsl has only one line and no template rules:

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

When you process mammals.xml with this stylesheet:

xalan mammals.xml blank.xsl

Xalan finds no explicit templates for the element nodes in mammals.xml, so the built-in template rules kick in, producing the following result:

<?xml version="1.0" encoding="UTF-8"?>      American Bison  American black bear  Bighorn sheep  Bobcat  Common gray fox  Cougar  Coyote  Gray wolf  Mule deer  Pronghorn  White-tailed deer

The built-in rules processed the root node, the element nodes, and all the text nodes that it found in mammals.xml. If you use an explicit template for just one of the nodes, that node will be processed with that template, but all the other nodes will be processed with the built-in templates.

The stylesheet built-in.xsl has only one template, and that template matches only one element node, the sixth mammal child of mammals, in mammals.xml:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>     <xsl:template match="mammals/mammal[6]">  Found <xsl:value-of select="."/>! </xsl:template>     </xsl:stylesheet>

Process mammals.xml with built-in.xsl with:

xalan mammals.xml built-in.xsl

and you will get this output:

 American Bison  American black bear  Bighorn sheep  Bobcat  Common gray fox      Found Cougar!  Coyote  Gray wolf  Mule deer  Pronghorn  White-tailed deer

When the processor encounters the node pattern matched in the template, it instantiates the template (including whitespace), but it also applies the built-in rules. You can, in effect, shut off the built-in rules by matching the unwanted nodes with an empty template matching mammal, as does shutoff.xsl, shown in Example 10-15.

Example 10-15. A stylesheet that shuts off a rule
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>     <xsl:template match="mammals">  <xsl:apply-templates select="mammal"/> </xsl:template>     <xsl:template match="mammal"/> <xsl:template match="mammal[6]">  Found <xsl:value-of select="."/>! </xsl:template>     </xsl:stylesheet>

When applied to mammals.xml:

xalan mammals.xml built-in.xsl

the result is just:

Found Cougar!

The difference is that when the processor encounters the first template, it searches for all templates that match mammal. Although both templates in the stylesheet match mammal, they are distinct because only one has a predicate that matches the sixth mammal node and instantiates some literal text. The other template instructs the processor to do nothing with all mammal nodes. (This does not cause an error because the more specific template that matches mammal[6] has priority over the template that matches only mammal.)

Notice the difference in the final stylesheet in this section, cougar.xsl, shown in Example 10-16.

Example 10-16. A stylesheet that only reports the cougar
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>     <xsl:template match="mammals"> <north.american>  <mammal>   <cat><xsl:apply-templates select="mammal[6]"/></cat>  </mammal> </north.american> </xsl:template>     </xsl:stylesheet>

This single template processes the children of mammals, but it selects only the sixth mammal element, in document order, and discards the others. When processed with mammals.xml, like this:

xalan -i 1 mammals.xml cougar.xsl

you get the following result:

<?xml version="1.0" encoding="UTF-8"?> <north.american>  <mammal>   <cat>Cougar</cat>  </mammal> </north.american>

I have mostly shown examples of the built-in rules working with element nodes, text nodes, and the root node. Table 10-1 summarizes what all the behaviors of the built-in template rules are when they encounter each of the seven nodes.

Table 10-1. Built-in template rule behavior

Node

Behavior

Root

Processes all children

Element

Processes all children, including the text nodes; the built-in rule for text copies text through

Attribute

Copies text through

Text

Copies text through

Comment

Nothing

Processing-instruction

Nothing

Namespace

Nothing

Section 5.8 of the XSLT specification discusses built-in template rules in greater detail. (Information on North American mammals was taken from the web site of the Smithsonian Institution's National Museum of Natural History at http://web6.si.edu/np_mammals/index.htm.)



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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