[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Cascading Menu (with Open/Closed Icons) and displayed children


Subject: [xsl] Cascading Menu (with Open/Closed Icons) and displayed children
From: Brian K Bonner <brian.bonner@xxxxxxxxxxxx>
Date: Thu, 27 Mar 2003 18:57:29 -0500

OK,  I've been researching this in the archive and I can't find a match 
and I'm stumped.  I want to create a menu of the sort:

Parameter selected (item 2.1) displays:

C item 1
O item 2
        O item 2.1 *
                C item 2.1.1
                C item 2.1.2
                C item 2.1.3
C item 3
C item 4


Basically, I want to specify a parameter and have it display the cascaded 
menu for me.   I want it to display the menu items at the top level and 
for any item that is selected (e.g. item 2.1 above),  I want it to display 
the open folder for that item and the closed folder for it's children.

The XSL almost works.  I'm able to display the tree, but when the selected 
item has a sibling with children, I get erroneous results.  The selected 
items children plus it's sibling's children shows up in the view.  Can 
anyone point out why?

If you change the param to 1.1, it works fine.  But leaving it at 2.1.1 
gives bad results.  Thanks.


Brian




Here is the XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0" xmlns:xalan="http://xml.apache.org/xslt">
    <xsl:output method="html" indent="yes" />

    <!-- this variable will need to be set by the document -->
    <xsl:param name="selection">Topic 2.1.1</xsl:param>

    <xsl:template match="menu">
        <!-- put all of the items under menu in a table -->
        <!-- remove the html and body tags later... -->
        <html>
            <body>
                <table border="0" cellspacing="0" cellpadding="0">
                    <xsl:apply-templates 
select="//menuItem"></xsl:apply-templates>
                </table>
            </body>
        </html>
    </xsl:template>



    <!-- these are the SELECTED items -->

    <xsl:template match="menuItem[description=$selection]">
        <tr>
            <td>
                <xsl:for-each select="./ancestor::menuItem">
                    <img>
                        <xsl:attribute 
name="src">images/spacer.gif</xsl:attribute>
                    </img>
                </xsl:for-each>
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="url" />
                    </xsl:attribute>

                    <xsl:apply-templates select="imageOpen" />
                    <xsl:apply-templates select="description" />
                </a>
                <xsl:apply-templates select="descendant::menuItem" />

            </td>
        </tr>
    </xsl:template>


    <!-- the ROOT items -->
    <xsl:template match="/menu/menuItem">
        <tr>
            <td>
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="url" />
                    </xsl:attribute>


                    <xsl:choose>
                        <xsl:when 
test="descendant::description=$selection">
                            <xsl:apply-templates select="imageOpen" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:apply-templates select="imageBase" />
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:apply-templates select="description" />
                </a>
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="menuItem">
        <xsl:if test="descendant::description=$selection">
            <!-- these are the items leading up to the selected item PATH 
-->
            <tr>
                <td>
                    <xsl:for-each select="./ancestor::menuItem">
                        <img>
                            <xsl:attribute 
name="src">images/spacer.gif</xsl:attribute>
                        </img>
                    </xsl:for-each>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="url" />
                        </xsl:attribute>
                        <xsl:apply-templates select="imageOpen" />
                        <xsl:apply-templates select="description" />
                    </a>
                </td>
            </tr>
        </xsl:if>
        <xsl:if test="../description=$selection">
            <!-- these items are the CHILDREN of the selected item -->
            <tr>
                <td>
                    <xsl:for-each select="./ancestor::menuItem">
                        <img>
                            <xsl:attribute 
name="src">images/spacer.gif</xsl:attribute>
                        </img>
                    </xsl:for-each>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="url" />
                        </xsl:attribute>
                        <xsl:apply-templates select="imageBase" />
                        <xsl:apply-templates select="description" />
                    </a>
                </td>
            </tr>
        </xsl:if>
    </xsl:template>

    <xsl:template match="imageBase">
        <img>
            <xsl:attribute name="src">
                <xsl:text />
                <xsl:value-of select="." />
            </xsl:attribute>
            <xsl:attribute name="border">0</xsl:attribute>
        </img>

    </xsl:template>

    <xsl:template match="imageOpen">
        <img>
            <xsl:attribute name="src">
                <xsl:text />
                <xsl:value-of select="." />
            </xsl:attribute>
            <xsl:attribute name="border">0</xsl:attribute>
        </img>

    </xsl:template>


    <xsl:template match="description">
        <xsl:value-of select="." />
    </xsl:template>


</xsl:stylesheet>



here is the sample data.


<?xml version="1.0"?>
<menu>
    <menuItem id="docid1">
        <description>Topic 1</description>
        <imageBase>images/closed.gif</imageBase>
        <imageOpen>images/open.gif</imageOpen>
        <url>http://test.com</url>

        <menuItem id="docid2">
            <description>Topic 1.1</description>
            <imageBase>images/closed.gif</imageBase>
            <imageOpen>images/open.gif</imageOpen>
                <url>http://test.com</url>
        </menuItem>
        <menuItem id="docid3">
            <description>Topic 1.2</description>
            <imageBase>images/closed.gif</imageBase>
            <imageOpen>images/open.gif</imageOpen>
        </menuItem>
    </menuItem>
    <menuItem id="docid4">
        <description>Topic 2</description>
        <imageBase>images/closed.gif</imageBase>
        <imageOpen>images/open.gif</imageOpen>
        <menuItem id="docid5">
            <description>Topic 2.1</description>
            <imageBase>images/closed.gif</imageBase>
            <imageOpen>images/open.gif</imageOpen>
            <menuItem id="docid2">
                <description>Topic 2.1.1</description>
                <imageBase>images/closed.gif</imageBase>
                <imageOpen>images/open.gif</imageOpen>
                <menuItem id="docid2">
                    <description>Topic 2.1.1.1</description>
                    <imageBase>images/closed.gif</imageBase>
                    <imageOpen>images/open.gif</imageOpen>
                </menuItem>
                <menuItem id="docid3">
                    <description>Topic 2.1.1.2</description>
                    <imageBase>images/closed.gif</imageBase>
                    <imageOpen>images/open.gif</imageOpen>
                </menuItem>

            </menuItem>
            <menuItem id="docid3">
                <description>Topic 2.1.2</description>
                <imageBase>images/closed.gif</imageBase>
                <imageOpen>images/open.gif</imageOpen>
                <menuItem id="docid2">
                    <description>Topic 2.1.2.1</description>
                    <imageBase>images/closed.gif</imageBase>
                    <imageOpen>images/open.gif</imageOpen>
                </menuItem>
                <menuItem id="docid3">
                    <description>Topic 2.1.2.2</description>
                    <imageBase>images/closed.gif</imageBase>
                    <imageOpen>images/open.gif</imageOpen>
                </menuItem>

            </menuItem>
        </menuItem>
        <menuItem id="docid6">
            <description>Topic 2.2</description>
            <imageBase>images/closed.gif</imageBase>
            <imageOpen>images/open.gif</imageOpen>
        </menuItem>
    </menuItem>
    <menuItem id="docid7">
        <description>Topic 3</description>
        <imageBase>images/closed.gif</imageBase>
        <imageOpen>images/open.gif</imageOpen>
        <menuItem id="docid8">
            <description>Topic 3.1</description>
            <imageBase>images/closed.gif</imageBase>
            <imageOpen>images/open.gif</imageOpen>
        </menuItem>
        <menuItem id="docid9">
            <description>Topic 3.2</description>
            <imageBase>images/closed.gif</imageBase>
            <imageOpen>images/open.gif</imageOpen>
        </menuItem>
    </menuItem>
</menu>

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords
xsl