[xsl] Need Help

Subject: [xsl] Need Help
From: iwantto keepanon <iwanttokeepanon@xxxxxxxx>
Date: Fri, 15 Dec 2006 05:12:30 +0300 (MSK)
(Note: re-posting a bounced message from xsl-list/gmail ...
 why? asked in another post.)


On 12/8/06, Brant Schroeder <brantschr@xxxxxxxxx> wrote:

>    I am trying to create a 3 tier menu using xml and xslt.
> I am very new to this and am having a lot of trouble.

>    Is this possible?  Also I would like to apply CSS Classes
> to certain nodes depending if they are selected or not.  And
> can I hide child nodes if the p=arent node is not selected?
> Any help would be appreciated.  I am using ASP.Net 2.0 to do
> this and it is for site I would like to use to display my school
> work and resume.  If it can't be done that is fine I wanted a
> dynamic menu using XML.


I am assuming you dont want to get into servlets or Ajax or the
like?  It can still be done w/ a trip to the server and generating
different HTML for the browser.  A good way to make things appear
and disappear is to use javascript b/c this is "behaviour" and that
is JS area of expertise.  But the following would work.  It basically
tells the XSLT what "submenu" you clicked on via the URL and the
XSLT uses that to either generate the submenu items or not.

I had to refactor your XML a bit to make the code flow correctly.
Basically item(s) dont contain item(s), menus and submenus contain
items and they are quite different in behaviour and looks.

The XSLT should work for you, the only question is what language are
you using and how are you going to get a URL value into your XSLT
processor?  The group should be able to help with that.  I did not
bother to escape the spaces in your XML/url(s), that can be handled
after you decide to use or not use this example.

(disclaimer: my style may not be one you want to emulate?  i have
been doing this off and on for a year and dont claim to be an expert!)


XML:
<?xml version="1.0" encoding="UTF-8"?>
<menu title="My World">
  <item url="#--real-Home-url--">Home</item>
  <submenu title="My Stuff">
    <item url="#--real-Backgrounds-url--">Backgrounds</item>
    <item url="#--real-Flyers-url--">Flyers</item>
    <item url="#--real-Posters-url--">Posters</item>
  </submenu>

  <submenu title="Portfolio">
    <item url="#--real-Backgrounds-url--">Backgrounds</item>
    <item url="#--real-Flyers-url--">Flyers</item>
    <item url="#--real-Posters-url--">Posters</item>
    <submenu title="More Information">
      <item url="#--real-Resume-url--">Resume</item>
      <item url="#--real-ContactMe-url--">Contact Me</item>

    </submenu>
  </submenu>
</menu>

XSL:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xhtml="http://www.w3.org/1999/xhtml"; version="1.0" exclude-result-prefixes="xhtml">
  <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; encoding="utf-8" indent="yes"/>

<!--
   All the unique "pos"ition values for sample menu:
-->
<!-- xsl:param name="pos">/</xsl:param -->
<!-- xsl:param name="pos">/My Stuff</xsl:param -->
<!-- xsl:param name="pos">/Portfolio</xsl:param -->
<xsl:param name="pos">/Portfolio/More Information</xsl:param>

<!--
   This will be your cgi program (or whatever you are generating the html with):
-->
  <xsl:param name="menu-url">#</xsl:param>

  <xsl:template match="/">

    <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en-US" lang="en-US">
      <head>
        <title>
          <xsl:value-of select="/menu/@title"/>
        </title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template xmlns="http://www.w3.org/1999/xhtml"; match="menu">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>

  <xsl:template xmlns="http://www.w3.org/1999/xhtml"; match="submenu">
    <xsl:param name="curr"/>

    <xsl:variable name="this-curr" select="concat( $curr, '/', @title )"/>
    <li>
      <a href="{$menu-url}?pos={$this-curr}">
        <xsl:value-of select="@title"/>
      </a>
      <xsl:if test="starts-with( $pos, $this-curr )">
        <ul>
          <xsl:apply-templates>
            <xsl:with-param name="curr" select="$this-curr"/>
          </xsl:apply-templates>
        </ul>
      </xsl:if>
    </li>
  </xsl:template>

  <xsl:template xmlns="http://www.w3.org/1999/xhtml"; match="item">
    <li>
      <a href="{@url}">
        <xsl:value-of select="text()"/>
      </a>
    </li>
  </xsl:template>
</xsl:stylesheet>

-- 
Rodman

Current Thread