XML Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

XML Element Transformation Chains

This section provides a tutorial example on how an XSL processor uses a transformation chain to perform a transformation inside another transformation.

When the XSL processor is transforming an element with a template, which 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 nested methods execution chain formed when running a Java program.

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

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

Let's test transform this XML document with the default template with this stylesheet, tree.xsl:

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

Now open tree.xml with Internet Explorer, you should get:

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

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.

Sections in This Chapter

What Is XSLT (XSL Transformations)?

"stylesheet" - The Stylesheet Declaration Element

"template" - The Template Declaration Element

Including Literal XML Elements in Templates

"apply-templates" - The Child Transformation Call Element

How the Transformation Process Gets Started?

Default Transformation Template

XML Element Transformation Chains

XML Element Transformation Chains - Complex Example

Dr. Herong Yang, updated in 2009
XML Element Transformation Chains