XML technologies / XSLT / XSLT Kochbuch / Selecting and traversing / Performing a postorder traversal
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You want to recursively process an element’s children first and then the element itself.
Solution
Solutions to this recipe have the following general form:
<xsl:template match="node( )">
<!--Verarbeiten der Kinder -->
<xsl:apply-templates/>
<!-- Irgendetwas mit dem aktuellen Knoten tun -->
</xsl:template>Discussion
The term postorder (post-ordering) is a computer science term for traversing a tree by recursively visiting the root’s children in postorder before visiting the root itself. This algorithm produces a stylesheet that processes the outermost leaf nodes and then works upward toward the document root.
You can apply a postorder traversal to the organization chart (orgchart.xml) to describe who reports to whom, starting at the bottom, as demonstrated in the next example. The example after that shows the output.
Example: stylesheet.
<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:apply-templates/>
<xsl:value-of select="@name"/><xsl:text> ist der Chef des Unternehmens. </xsl:text>
<xsl:call-template name="reportsTo"/>
<xsl:call-template name="HimHer"/> <xsl:text>. </xsl:text>
<xsl:text>

</xsl:text>
</xsl:template>
<xsl:template match="employee[employee]">
<xsl:apply-templates/>
<xsl:value-of select="@name"/><xsl:text> ist ein Manager. </xsl:text>
<xsl:call-template name="reportsTo"/>
<xsl:call-template name="HimHer"/> <xsl:text>. </xsl:text>
<xsl:text>

</xsl:text>
</xsl:template>
<xsl:template match="employee">
<xsl:text>Niemand berichtet </xsl:text>
<xsl:value-of select="@name"/><xsl:text>. 
</xsl:text>
</xsl:template>
<xsl:template name="HimHer">
<xsl:choose>
<xsl:when test="@sex = 'male' ">
<xsl:text>ihm</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>ihr</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="reportsTo">
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="position() < last() 1 and last() > 2">
<xsl:value-of select="@name"/><xsl:text>, </xsl:text>
</xsl:when>
<xsl:when test="position() = last() - 1 and last() > 1">
<xsl:value-of select="@name"/><xsl:text> and </xsl:text>
</xsl:when>
<xsl:when test="position() = last() and last() = 1">
<xsl:value-of select="@name"/><xsl:text> berichtet </xsl:text>
</xsl:when>
<xsl:when test="position() = last()">
<xsl:value-of select="@name"/><xsl:text> berichten </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>Example: output.
Niemand berichtet Phill McKraken.
Niemand berichtet Betsy Ross.
Ima Little ist ein Manager. Betsy Ross berichtet ihr.
Nancy Pratt ist ein Manager. Phill McKraken und Ima Little berichten ihr.
Niemand berichtet Walter H. Potter.
Niemand berichtet Craig F. Frye.
Niemand berichtet Hardy Hamburg.
Niemand berichtet Rich Shaker.
Wendy B.K. McDonald ist ein Manager. Craig F. Frye, Hardy Hamburg und Rich Shaker berichten ihr.
Jane Doe ist ein Manager. Walter H. Potter und Wendy B.K. McDonald berichten ihr.
Niemand berichtet Allen Bran.
Niemand berichtet Frank N. Berry.
Niemand berichtet Jack Apple.
Cindy Post-Kellog ist ein Manager. Allen Bran, Frank N. Berry und Jack Apple berichten ihr.
Niemand berichtet R.P. McMurphy.
Jack Nicklaus ist ein Manager. R.P. McMurphy berichtet ihm.
Niemand berichtet Forrest Gump.
Niemand berichtet Andrew Beckett.
Tom Hanks ist ein Manager. Forrest Gump und Andrew Beckett berichten ihm.
Niemand berichtet Helen Prejean.
Susan Sarandon ist ein Manager. Helen Prejean berichtet ihr.
Oscar A. Winner ist ein Manager. Jack Nickolas, Tom Hanks und Susan Sarandon berichten ihm.
Mike Rosenbaum ist ein Manager. Cindy Post-Kellog und Oscar A. Winner berichten ihm.
Jil Michel ist der Chef des Unternehmens. Nancy Pratt, Jane Doe und Mike Rosenbaum berichten ihr.
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