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

Notice that the output format is much better now. Why? I have inserted a pair of HTML tags, <pre> and </pre> at the beginning and the end of the replacement content for the root element "p". It tells IE to present everything in between as pre-formatted text, meaning respect the white spaces. Then I copied and pasted the text out of the IE window into my note.

In order to understand how the output got produced, let's act like the XSL processor doing the transformation our self:

  • Transformation starts on the root element "p" automatically.
  • Template for element "p" is available, so we use it.
  • In template for element "p", text "Replacement p1." is reached, so send it to output.
  • Then the template calls for transforming element "c1", so we search for template for "c1".
  • Template for element "c1" is available, so we will go into it.
  • In template for element "c1", text "Replacement c11." is reached, so send it to output.
  • Then the templates calls for transforming element "g11", so we search for template for "g11".
  • But, no template for element "g11" is available, so we use the default template.
  • The default template does nothing but send the element's text content "Text g11." to output.
  • Then, we are reaching the text, "Replacement c12.", so send it to output.
  • Now the template for element "c1" calls for transforming element "g12", so we search for template for "g12".
  • Again, no template for element "g12" is available, so we use the default template.
  • The default template sends the text content of "g12", "Text g12.", to output.
  • The last thing in template for "c1" is the text, "Replacement c13.", so send it to output.
  • After finishing with template for "c1", we return to template for "p".
  • The next thing in template for "p" is the text, "Replacement p2.", so send it to output.
  • The next thing in template for "p" is a call to transform element "c2", so we search for template for element "c2".
  • Template for element "c2" is available, so we use it, and go into it.
  • In template for "c2", there is only one text, "Replacement c21.", so send it to output.
  • Then we return to the calling template for "p".
  • The next thing in template for "p" is the text, "Replacement p3.", so send it to output.
  • Now, the template for element "p" calls for transformation for element "c3", so search for template for element "c3".
  • But again, no template is available for element "c3", so we use the default template.
  • However, there are two elements named by "c3", so we have to transform both of them.
  • The default template sends the first text content of the first element "c3", "Text c31.", to output.
  • Then, the default template calls for transformation on the first child element "g21", so text content, "Text g31." is send to output.
  • Then, the default template sends the second text content, "Text c32.", to output.
  • Then, the default template moves to the second child element "g22", and sends text, "Text g32.", to output.
  • Then, the default template sends the third text content, "Text c33.", to output.
  • Now, we are done with the first element "c3", we now transform the second element "c3".
  • Again, we need to use the default template, which sends the text, "Text c41." to output.
  • Now, we are back in template for "p", reaches text, "Replacement p4.", and sends it to output.
  • Then, template for "p" calls for transformation for element "c3" again.
  • Of course, we need to repeat what we just did, sending "Text c31.", "Text g31.", "Text c32.", "Text g32.", "Text c33.", and "Text c41." to output.
  • Then, we are back in template for "p", reaches text, "Replacement p5.", and sends it to output.
  • That's it. Done.

If you can follow me through those steps, you truelly understand now how XSL processor working from one template to another template, starting from the root template.

Conclusion:

  • No need to write "apply-templates" statement for the root element.
  • If no "template" defined for an element, a default template will be used to transform the element to its text content.
  • Transformations are performed one inside another as transformation chain.

Exercise: Write a template that produces no output.

Part:   1  2  3  4  

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