Re: [xsl] Process Children Before Parents Recursively!

Subject: Re: [xsl] Process Children Before Parents Recursively!
From: Stuart Jansen <sjansen@xxxxxxxxxxxx>
Date: Mon, 18 Jul 2005 11:18:09 -0600
On Mon, 2005-07-18 at 17:26 +1000, Adam J Knight wrote:
> I would really appreciate if some one can demonstrate how I would alter the
> logic of this style sheet, so that all child menus are generated before
> their parents (recursively). Thus I should be error free.
> 
> I would appreciate any help!!!!!!!!!!

The trick is to make the recursive call before producing any output.
There's alot that could be done to clean up your stylesheet, but with
minimal changes to it, here's a version that should do what you want:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:param name="url"/>
  <xsl:output method="html" encoding="ISO-8859-1"/>

  <xsl:template match="/dfile">
  <script language="javascript">
    function loadMenus() {
        <xsl:call-template name="sub_menus"/>
        writeMenus();
    }
  </script>
  <script language="JavaScript">loadMenus();</script>
  </xsl:template>

  <xsl:template name="sub_menus">
    <xsl:for-each select="df_data_row">
        <xsl:if test="child::df_data_row">
        <xsl:call-template name="sub_menus"/>
        window.mnu_<xsl:value-of select="document_id"/> = new
Menu('<xsl:value-of select="name"/>','Verdana, Arial, Helvetica,
sans-serif',11,'ffffff','000033','cce3f8','ffffff','left','top',2,2,300,2,1,
true,true,true,0);

<xsl:call-template name="menu_items"/>
        </xsl:if>
    </xsl:for-each>
  </xsl:template>

<xsl:template name="menu_items">
  <xsl:for-each select="child::df_data_row">
    <xsl:choose>
        <xsl:when test="child::df_data_row">
          mnu_<xsl:value-of
select="parent::df_data_row/document_id"/>.addMenuItem(mnu_<xsl:value-of
select="document_id"/>,"document.location.href='index.php?pg=<xsl:value-of select="document_id"/>'");
        </xsl:when>
        <xsl:otherwise>
          mnu_<xsl:value-of
select="parent::df_data_row/document_id"/>.addMenuItem("<xsl:value-of
select="name"/>", "document.location.href='index.php?pg=<xsl:value-of
select="document_id"/>'" );
        </xsl:otherwise>
   </xsl:choose>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

-- 
Stuart Jansen <sjansen@xxxxxxxxxxxx>
Guru Labs, L.C.

Current Thread