Performing a Postorder Traversal

Problem

You want to recursively process the children of an element first and then the element itself.

Solution

Solutions to this recipe have the following general form:

<xsl:template match="node(  )">
     <!--Process children -->
     <xsl:apply-templates/>
   
     <!-- Do something with current node -->
   
</xsl:template>

Discussion

The term postorder is computer-science jargon for traversing a tree so that you recursively visit the children of the root in postorder and then visit the root. This algorithm produces a stylesheet that processes the outermost leaf nodes and works its way up to the document root.

You can apply a postorder traversal to the organizational chart (orgchart.xml) to produce an explanation of who reports to whom, starting from the bottom, as shown in Example 4-21. Example 4-22 shows the output.

Example 4-21. 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> is the head of the company. </xsl:text>
     <xsl:call-template name="reportsTo"/>
     <xsl:call-template name="HimHer"/> <xsl:text>. </xsl:text>
     <xsl:text>&#xa;&#xa;</xsl:text>
</xsl:template>
     
<xsl:template match="employee[employee]">
     <xsl:apply-templates/>
     <xsl:value-of select="@name"/><xsl:text> is a manager. </xsl:text>
     <xsl:call-template name="reportsTo"/>
     <xsl:call-template name="HimHer"/> <xsl:text>. </xsl:text>
     <xsl:text>&#xa;&#xa;</xsl:text>
</xsl:template>
   
<xsl:template match="employee">
     <xsl:text>Nobody reports to </xsl:text>
     <xsl:value-of select="@name"/><xsl:text>. &#xa;</xsl:text>
</xsl:template>
   
<xsl:template name="HimHer">
     <xsl:choose>
       <xsl:when test="@sex = 'male' ">
         <xsl:text>him</xsl:text>
       </xsl:when>
       <xsl:otherwise>
         <xsl:text>her</xsl:text>
       </xsl:otherwise>
     </xsl:choose>
</xsl:template>
     
<xsl:template name="reportsTo">
     <xsl:for-each select="*">
       <xsl:choose>
         <xsl:when test="position(  ) &lt; 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> reports to </xsl:text>
         </xsl:when>
         <xsl:when test="position(  ) = last(  )">
          <xsl:value-of select="@name"/><xsl:text> report to </xsl:text>
         </xsl:when>
         <xsl:otherwise>
          <xsl:value-of select="@name"/>
         </xsl:otherwise>
       </xsl:choose> 
     </xsl:for-each>
</xsl:template>
          
</xsl:stylesheet>

Example 4-22. Output

Nobody reports to Phill McKraken. 
Nobody reports to Betsy Ross. 
Ima Little is a manager. Betsy Ross reports to her. 
   
Nancy Pratt is a manager. Phill McKraken and Ima Little report to her. 
   
Nobody reports to Walter H. Potter. 
Nobody reports to Craig F. Frye. 
Nobody reports to Hardy Hamburg. 
Nobody reports to Rich Shaker. 
Wendy B.K. McDonald is a manager. Craig F. Frye, Hardy Hamburg and Rich Shaker report to 
her. 
   
Jane Doe is a manager. Walter H. Potter and Wendy B.K. McDonald report to her. 
   
Nobody reports to Allen Bran. 
Nobody reports to Frank N. Berry. 
Nobody reports to Jack Apple. 
Cindy Post-Kellog is a manager. Allen Bran, Frank N. Berry and Jack Apple report to her. 
   
Nobody reports to R.P. McMurphy. 
Jack Nickolas is a manager. R.P. McMurphy reports to him. 
   
Nobody reports to Forest Gump. 
Nobody reports to Andrew Beckett. 
Tom Hanks is a manager. Forest Gump and Andrew Beckett report to him. 
   
Nobody reports to Helen Prejean. 
Susan Sarandon is a manager. Helen Prejean reports to her. 
   
Oscar A. Winner is a manager. Jack Nickolas, Tom Hanks and Susan Sarandon report to him. 
   
Mike Rosenbaum is a manager. Cindy Post-Kellog and Oscar A. Winner report to him. 
   
Jil Michel is the head of the company. Nancy Pratt, Jane Doe and Mike Rosenbaum report to 
her.

Get XSLT Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.