ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Menu and submenu dependent on current page
Submitter: Sergey Yushckeyev (other recipes)
Last Updated: 2006/10/24
Version no: 1.0
Category: Embedded Scripts

 

Not Rated yet


Description:

Sometimes you have tree-like menu and you want to present it as Main menu and show custom sub menu on page that is dependent on current page. Here it is.

Source: Text Source

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
    <curpage>myphotos</curpage>
    <items>
      <item name="home" title="Home" uri="/index.php" />
      <item name="viewprofile" title="My Profile" uri="/mypage/profile.php">
        <submenu>
          <subitem name="viewprofile" title="View my profile" uri="/mypage/profile.php" />
          <subitem name="editprofile" title="Edit my profile" uri="/mypage/profile.php?mode=edit" />
          <subitem name="myphotos" title="Upload photos" uri="/mypage/profile.php?mode=photos" />
        </submenu>
      </item>
    </items>
</menu>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="UTF-8" indent="no"/>

  <xsl:template match="/">
  <xsl:param name="curpagename" select="curpage" />
    <table>
      <tr>
        <xsl:for-each select="items/item">
          <xsl:variable name="curitempagename" select="name"/>
          <xsl:choose>
            <xsl:when test="$curpagename = $curitempagename">
              <xsl:call-template name="menuitem" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="count(submenu/subitem) &gt; 0">
                  <xsl:call-template name="menuitem">
                    <xsl:with-param name="active">
                      <xsl:call-template name="isInSubmenu">
                        <xsl:with-param name="list" select="submenu/subitem" />
                        <xsl:with-param name="pos" select="1"/>
                        <xsl:with-param name="curpagename" select="$curpagename" />
                      </xsl:call-template>
                    </xsl:with-param>
                  </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="menuitem">
                    <xsl:with-param name="active" select="0" />
                  </xsl:call-template>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </tr>
    </table>
  </xsl:template>
  
  <xsl:template name="menuitem">
    <xsl:param name="active" select="1" />
    <td>
      <xsl:choose>
        <xsl:when test="$active = 1">
          <xsl:attribute name="class">mainmenuitemactive</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">mainmenuitem</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <a href="{uri}"><xsl:value-of select="title" /></a>
    </td>
  </xsl:template>
  
  <xsl:template name="isInSubmenu">
    <xsl:param name="list"/>
    <xsl:param name="pos"/>
    <xsl:param name="curpagename" />
    <xsl:variable name="curitemname" select="$list[$pos]/name" />
    <xsl:choose>
      <xsl:when test="$curitemname != $curpagename">
        <xsl:choose>
          <xsl:when test="$pos &lt; count($list)">
            <xsl:call-template name="isInSubmenu">
              <xsl:with-param name="list" select="$list" />
              <xsl:with-param name="pos" select="$pos + 1"/>
              <xsl:with-param name="curpagename" select="$curpagename" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>

The license for this recipe is available here.

Discussion:

I think it's not very difficult to write stylesheet for submenu with using of

I'm new in XSLT world (1+ month), so it would be pleasure to read your comments and suggestions.



Add comment

No comments.



Highest rated recipes:

1. Search and Replace

2. Generating a newline

3. Internationalization ...

4. Restricting processing ...

5. Result Pagination with ...

6. Fetching information ...

7. Getting text children of ...

8. Creating empty elements




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.