Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Philip 21 posts 41 karma points
    Jul 20, 2011 @ 11:00
    John Philip
    0

    Xslt and Superfish menu

    Hullo.

    My client doesn't want the parent link of a sub menu go to a page. So I thought to change the href to a hash (#).
    The superfish javascript generates a useful "sf-with-ul" class on the "a" element that I want to change the attribute of the href to #.

    I was thinking I need something like this but I can't get it to work and don't really know at which place to insert it :

    <xsl:if test="@class = sf-with-ul">
      <xsl:attribute name="href">#</xsl:attribute>
    </xsl:if>

    Please could someone help me?


    Here is the xslt code I got off the forums which worked for generating my menu.


    <xsl:param name="currentPage"/>

    <!-- update this variable on how deep your site map should be -->
    <xsl:variable name="maxLevelForSitemap" select="4"/>

      <xsl:template match="/">
        <div id="menu">
          <xsl:call-template name="drawNodes">
            <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
          </xsl:call-template>
        </div>    

      </xsl:template>

      <xsl:template name="drawNodes">
        <xsl:param name="parent"/>
        <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">

          <ul>
            <xsl:if test="$parent/@level = 1">
               <xsl:attribute name="class">sf-menu</xsl:attribute>
            <li class="home"><a href="/">Home</a></li>  
              
            </xsl:if>

            <xsl:for-each select="$parent/* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevelForSitemap]">
              <li>
                <xsl:if test="$currentPage/@id = current()/@id">
                        <xsl:attribute name="class">current</xsl:attribute>
                </xsl:if>

                <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a>
                
                <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevelForSitemap]) &gt; 0">
                  <xsl:call-template name="drawNodes">
                    <xsl:with-param name="parent" select="."/>
                  </xsl:call-template>
                </xsl:if>
                
              </li>
            </xsl:for-each>
          </ul>
        </xsl:if>

      </xsl:template>
        
    </xsl:stylesheet>
  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 20, 2011 @ 12:02
    Fuji Kusaka
    0

     

    Hi John,

    Am not sure what you are looking for, are you trying to rewrite this

    <xsl:iftest="@class = sf-with-ul">
    <xsl:attributename="href">#</xsl:attribute>
    </xsl:if>

    to

     

     

    <xsl:iftest="@class = sf-with-ul">
     
    <xsl:element name="a">
    <xsl:attributename="href">   
    <text>#</text>
    </xsl:attribute>
    <
    /xsl:element>
    </xsl:if>

     

     

     

  • John Philip 21 posts 41 karma points
    Jul 20, 2011 @ 12:47
    John Philip
    0


    Yes, I'm needing this:

    <a href="file-name.aspx" class="sf-with-ul">pagename</a>

    to be rewritten like this:

    <a href="#" class="sf-with-ul">pagename</a>

    My concern is that the class "sf-with-ul" is generated through some javascript. So I don't know how if the xslt can find that? If that is the problem, how do I find that element to rewrite it in xslt?

    Desired end result:

      <ul>
        <li><a href="file-name.aspx">pagename</a></li>
        <li><a href="#" class="sf-with-ul">pagename</a>
          <ul>
            <li><a href="file-name.aspx">pagename</a></li>
            <li><a href="file-name.aspx">pagename</a></li>
          </ul>
        </li>
        <li><a href="file-name.aspx">pagename</a></li>
        <li><a href="file-name.aspx">pagename</a></li>
        <li><a href="file-name.aspx">pagename</a></li>
      </ul>



  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jul 20, 2011 @ 12:52
    Jan Skovgaard
    0

    Hi John

    To do the above you're thinking it would require that you got an attribute in the umbraco.config xml somewhere and it does not seem like you have that.

    Seems like you're mixing JavaScript and XSLT :-) I'm pretty sure the class is added by JavaScript and is therefore accessible by JavaScript only.

    Since you know that it's level 2 links you don't want links on you can for instance do a check on the level you'e at where the link is generated in your code.

    So instead of having

    You can write this

    <xsl:choose>
      <xsl:when test="@level = 2">
     <xsl:value-of select="@nodeName" />
     </xsl:when>
     <xsl:otherwise>
     <a href="{umbraco.library:NiceUrl(@id)}">
       <xsl:value-of select="@nodeName"/>
      </a>
    </xsl:otherwise>
    </xsl:choose>

    I think this should work for you.

    /Jan

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 20, 2011 @ 13:01
    Fuji Kusaka
    0

     

    John, Try this out 

    <a>
     
    <
    xsl:attribute name="href">   
              
    <xsl:text>#</xsl:text>
     </xsl:attribute>

    <xsl:attribute name="class">
    <xsl:text>sf-with-ul</xsl:text>
    </xsl:attribute>
    <xsl:value-of select="@nodeName" />
     
    <
    /a>  

     

    //Fuji

     

  • John Philip 21 posts 41 karma points
    Jul 20, 2011 @ 13:09
    John Philip
    0

    Hi Jan, you're absolutely right. However I don't want to change ALL the links at level 2. How do I say only change the links that have a child list? Sorry, as you can see I really don't know my xslt syntax.

    Thanks for you responses Fuji, I think its the choose/testing that I'm stuggling with as Jan pointed out.

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 20, 2011 @ 13:11
    Fuji Kusaka
    0

    No worries John, I miss undestood your request. I thought you only wanted to get this output.

     

  • John Philip 21 posts 41 karma points
    Jul 21, 2011 @ 11:32
    John Philip
    0

    I eventually found the answer!

    This did the trick:

    *[@isDoc]

    So the full thing looked like this:

                <xsl:choose>
                  <xsl:when test="*[@isDoc]">
                    <a href="#">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                 </xsl:when>
                 <xsl:otherwise>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                </xsl:otherwise>
                </xsl:choose>

    http://our.umbraco.org/forum/developers/xslt/11008-how-do-i-check-if-a-node-has-children-in-45

Please Sign in or register to post replies

Write your reply to:

Draft