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...)

Transformation Chains

When the XSL processor is transforming an element with a template, if the template contains an "apply-templates" statement, the XSL processor will hold the transformation of the current element, and starts the transformation requested by the "apply-templates" statement. If the template used in the second transformation also contains another "apply-templates" statement, the XSL processor will hold the second transformation, and starts a third transformation. This processing pattern is forming a transformation chain, very much like the method execution chain formed when running a Java program.

Let's use the following XML file, tree.xml, to explore how XSL processor is performing transformations inside other transformations:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="tree.xsl"?>
<p>
 Text p1.
 <c1>
  Text c11.
  <g11>Text g11.</g11>
  Text c12.
  <g12>Text g12.</g12>
  Text c13.
 </c1>
 Text p2.
 <c2>
  Text c21.
  <g21>Text g21.</g21>
  Text c22.
  <g22>Text g22.</g22>
  Text c23.
 </c2>
 Text p3.
 <c3>
  Text c31.
  <g21>Text g31.</g21>
  Text c32.
  <g22>Text g32.</g22>
  Text c33.
 </c3>
 Text p4.
 <c3>
  Text c41.
 </c3>
 Text p5.
</p>

Test 1: Using default templates, tree.xsl:

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

openning tree.xml with Internet Explorer, I got:

Text p1. Text c11. Text g11. Text c12. Text g12. Text c13. Text p2. 
Text c21. Text g21. Text c22. Text g22. Text c23. Text p3. Text c31. 
Text g31. Text c32. Text g32. Text c33. Text p4. Text c41. Text p5. 

Note that:

  • The default template transforms elements by keeping only text parts of their contents.
  • The default template also requests to apply transformation on all child elements.
  • The output produced by Internet Explorer (IE) actually contains many white space characters, like line breaks. But IE can not store the output into a text file, it has to present the output to the Web browser window, where white spaces are ignored. This is why the output was captured here as a single line. I broke the line into 3 lines to fit the format of this note.

I have tried the "Save As" function in IE, but it offers only two file types to save: .xml and .xsl. So there is no way for me to save it as transformed text format. But don't worry, we will find another XSL processor that will allow us to save the transformation output as a text file.

Test 2: Using my own templates for some of the elements by revising tree.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="p">
  <pre>
  Replacement p1.
  <xsl:apply-templates select="c1"/>
  Replacement p2.
  <xsl:apply-templates select="c2"/>
  Replacement p3.
  <xsl:apply-templates select="c3"/>
  Replacement p4.
  <xsl:apply-templates select="c3"/>
  Replacement p5.
  </pre>
 </xsl:template>
 <xsl:template match="c1">
  Replacement c11.
  <xsl:apply-templates select="g11"/>
  Replacement c12.
  <xsl:apply-templates select="g12"/>
  Replacement c13.
 </xsl:template>
 <xsl:template match="c2">
  Replacement c21.
 </xsl:template>
</xsl:stylesheet>

openning tree.xml with Internet Explorer again, I got:

  Replacement p1.

  Replacement c11.
  Text g11.
  Replacement c12.
  Text g12.
  Replacement c13.

  Replacement p2.

  Replacement c21.

  Replacement p3.

  Text c31.
  Text g31.
  Text c32.
  Text g32.
  Text c33.

  Text c41.

  Replacement p4.

  Text c31.
  Text g31.
  Text c32.
  Text g32.
  Text c33.

  Text c41.

  Replacement p5.

(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