XML Element Transformation Chains - Complex Example

This section provides a tutorial example on how an XSL processor works through a more complex XSLT stylesheet to perform a transformation on an XML document tree in a transformation chain process.

Now let's do another test on the source XML document, tree.xml, presented in the previous section. This time, we going to use a more interesting stylesheet, 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>

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

  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.

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 result 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:

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

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

XSLT (XSL Transformations) Introduction

 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

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB