December 2002
Intermediate to advanced
672 pages
16h 53m
English
You want to recursively process the children of an element first and then the element itself.
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>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>

</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"/> ...
Read now
Unlock full access