XML technologies / XSLT / XSLT Kochbuch / Selecting and traversing / Performing a preorder traversal
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You want to process an element first, followed recursively by its child elements.
Solution
Solutions to this recipe have the following general form:
<xsl:template match="node( )">
<!-- Irgendetwas mit dem aktuellen Knoten tun -->
<!--Kinder verarbeiten -->
<xsl:apply-templates/>
</xsl:template>Discussion
The term preorder describes a tree traversal in computer science in which the root is visited first, followed recursively by its children in preorder. This is probably the most common way to process XML. This chapter examines two of the many applications of this idiom. You will frequently encounter this traversal method in recipes in later chapters.
Consider an organization chart (orgchart.xml) that encodes, in simplified form, that an employee element B is a child of another employee element A if B reports to A. The following example uses a preorder traversal to explain who manages whom. The next example shows the output.
Example: stylesheet.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/employee" priority="10">
<xsl:value-of select="@name"/><xsl:text> ist Chef des Unternehmens. </xsl:text>
<xsl:call-template name="HeShe"/><xsl:text> verwaltet </xsl:text>
<xsl:call-template name="manages"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="employee[employee]">
<xsl:value-of select="@name"/> <xsl:text> ist ein Manager. </xsl:text>
<xsl:call-template name="HeShe"/> <xsl:text> verwaltet </xsl:text>
<xsl:call-template name="manages"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="employee">
<xsl:value-of select="@name"/><xsl:text> hat keine Sorgen. </xsl:text>
<xsl:text>

</xsl:text>
</xsl:template>
<xsl:template name="HeShe">
<xsl:choose>
<xsl:when test="@sex = 'male' ">
<xsl:text>Er</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Sie</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="manages">
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="position() != last() and last() > 2">
<xsl:value-of select="@name"/><xsl:text>, </xsl:text>
</xsl:when>
<xsl:when test="position() = last() and last() > 1">
<xsl:text> und </xsl:text><xsl:value-of select="@name"/><xsl:text>. </xsl:text>
</xsl:when>
<xsl:when test="last() = 1">
<xsl:value-of select="@name"/><xsl:text>. </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>

</xsl:text>
</xsl:template>
</xsl:stylesheet>Example: output.
Jil Michel ist Chef des Unternehmens. Sie verwaltet Nancy Pratt, Jane Doe und Mike Rosenbaum.
Nancy Pratt ist ein Manager. Sie verwaltet Phill McKraken und Ima Little.
Phill McKraken muss sich um nichts kümmern.
Ima Little ist ein Manager. Sie verwaltet Betsy Ross.
Betsy Ross muss sich um nichts kümmern.
Jane Doe ist ein Manager. Sie verwaltet Walter H. Potter und Wendy B.K. McDonald.
Walter H. Potter muss sich um nichts kümmern.
Wendy B.K. McDonald ist ein Manager. Sie verwaltet Craig F. Frye, Hardy Hamburg und Rich Shaker.
Craig F. Frye muss sich um nichts kümmern.
Hardy Hamburg muss sich um nichts kümmern.
Rich Shaker muss sich um nichts kümmern.
Mike Rosenbaum ist ein Manager. Er verwaltet Cindy Post-Kellog und Oscar A. Winner.
Cindy Post-Kellog ist ein Manager. Sie verwaltet Allen Bran, Frank N. Berry und Jack Apple.
Allen Bran muss sich um nichts kümmern.
Frank N. Berry muss sich um nichts kümmern.
Jack Apple muss sich um nichts kümmern.
Oscar A. Winner ist ein Manager. Er verwaltet Jack Nickolas, Tom Hanks und Susan Sarandon.
Jack Nicklaus ist ein Manager. Er verwaltet R.P. McMurphy.
R.P. McMurphy muss sich um nichts kümmern.
Tom Hanks ist ein Manager. Er verwaltet Forrest Gump und Andrew Beckett.
Forrest Gump muss sich um nichts kümmern.
Andrew Beckett muss sich um nichts kümmern.
Susan Sarandon ist ein Manager. Sie verwaltet Helen Prejean.
Helen Prejean muss sich um nichts kümmern.
A more substantial application of preorder traversal is converting an expression tree into prefix notation. Using the MathML fragment in the following two examples, you can transform it into a Lisp-like syntax.
Example: MathML fragment representing x2+4x+4 = 0.
<apply>
<eq/>
<apply>
<plus/>
<apply>
<power/>
<ci>x</ci>
<cn>2</cn>
</apply>
<apply>
<times/>
<cn>4</cn>
<ci>x</ci>
</apply>
<cn>4</cn>
</apply>
<cn>0</cn>
</apply>Example: stylesheet for converting the MathML fragment into prefix notation.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="apply">
<xsl:value-of select="local-name(*[1])"/>
<xsl:text>(</xsl:text>
<xsl:apply-templates/>
<xsl:text>)</xsl:text>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<xsl:template match="ci|cn">
<xsl:value-of select="."/>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
</xsl:stylesheet>Example: output.
eq(plus(power(x,2),times(4,x),4),0)
The MathML converter is not the purest example of preorder traversal, mainly because MathML encodes mathematical expressions in an unconventional way. It essentially follows the preorder idiom because the code first processes the apply element (outputting the name of its first child element) and then recursively processes its child elements (using <xsl:apply-templates/>). The code following apply-templates inserts closing parentheses and commas where required, without causing any further traversal.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de