Herong's Tutorial Notes On XML Technologies
Dr. Herong Yang, Version 3.04

XSL - Declaring and Applying Templates

Part:   1  2   3  4 

(Continued from previous part...)

The 'apply-templates' Element

apply-templates: An XSL element serving as an action statement. It requests the XSL processor to apply transformation templates to certain elements in the source XML file.

<xsl:apply-templates select="pattern"/>
<xsl:apply-templates/>

where "pattern" is a pattern used to match element names in the source XML file.

An "apply-templates" statement is only used inside a "template" statement, and the "pattern" matching process is done in reference to the current element on which the "template" statement has been applied. For example, if "pattern" is an element name, it will be matched only against child elements of the current element.

The output of the requested transformation on the matched elements will be part of the replacement content of the current element.

If "pattern" matches no element, nothing will happen.

If "pattern" matches multiple elements, all of the matched elements will be transformed.

If attribute "select" is not specified, all child elements will be transformed.

Example:

 <xsl:template match="p">
  Text part 1.
  <xsl:apply-templates select="q"/>
  Text part 2.
 </xsl:template>

The output of the transformation on element "q" will be inserted between text part 1 and 2, when this template is used to transform element "p". Assuming element "q" is a child element of element "p".

How XSL Transformation Gets Started?

With "template" and "apply-templates" elements, we can now look at how the XSL transformation gets started, and in which order the elements in the source XML file get processed.

First, let's review the hello_xsl.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<p>Hello world!</p>

the hello.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="p">Hello world.! - From hello.xsl.</xsl:template>
</xsl:stylesheet>

and the output from Internet Explorer by openning hello_xsl.xml:

Hello world! - From hello.xsl.

Question: Where is the "apply-templates" statement for element "p"? Who requested to apply transformation template on element "p"? My guess is that the XSL processor automatically starts the transformation on the root element, which is "p" in our example.

Rule: XSL processors automatically start to apply transformation on the root element. The following XSL statements is not needed, also not allowed directly under "stylesheet":

<xsl:apply-templates select="root_element_name"/>

Default Transformation Template

Question: How an element will be transformed if there is no template defined for that element?

So let's modify hello.xsl to:

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

and view hello_xsl.xml in IE, you will get:

Hello world!

It seemed to me that the IE supplied a default transformation template.

Rule: XSL processors use a default transformation template for elements for which there is no template defined in the style sheet. The behavior of the default template is to copy only text part of the element's content to the output.

(Continued on next part...)

Part:   1  2   3  4 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XSL - Declaring and Applying Templates