Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Nov 18, 2009 @ 16:35
    Dan
    0

    Add class to first item in WebSitemap XSLT list

    Hi,

    This has got to be a simple one but I just can't get it.  I'm trying to add a class of 'tree' to just the first <ul> tag in my multilevel sitemap.  The code I have so far is this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <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="sitemap">
    <xsl:call-template name="drawNodes"> 
    <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@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="@level = 0">
                <xsl:attribute name="class"><xsl:text>tree</xsl:text></xsl:attribute>
            </xsl:if>
            <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForSitemap]">
                <li> 
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        <xsl:value-of select="@nodeName"/>
                    </a> 
                    <xsl:if test="count(./node [string(./data [@alias='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>

    I've added the line:

    <xsl:if test="@level = 0">
                <xsl:attribute name="class"><xsl:text>tree</xsl:text></xsl:attribute>
            </xsl:if>

    But this hasn't worked.  It doesn't error but it just doesn't add the class at all.

    Any ideas anyone?

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Nov 18, 2009 @ 16:45
    Chris Houston
    0

    Hi Dan,

    In your example you are passing in nodes where @level = 1 so it will never equal Zero :)

    What you can do is fix that ;-)

    Or...

    One way you can do this is pass into your drawNodes function another parameter, which is only passed in from the main template and not when the function calls itself.

    Add:

    <xsl:with-param name="first" select="1"/>

    Then check for it:

           <xsl:if test="$first = 1">    
               <xsl:attribute name="class">tree</xsl:attribute>   
           </xsl:if>   

    Cheers,

    Chris

  • dandrayne 1138 posts 2262 karma points
    Nov 18, 2009 @ 16:47
    dandrayne
    0

    Try testing for the level of the parent - if it's level one then add your class

    <xsl:if test="$parent/@level = 1">
    <xsl:attribute name="class"><xsl:text>tree</xsl:text></xsl:attribute>
    </xsl:if>
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 18, 2009 @ 16:50
    Dirk De Grave
    0

    Parent node is from level 1, so you can never get to a node having level = 0 using the code above.

    <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@level=1]"/>

     

    Cheers,

    /Dirk

  • Dan 1288 posts 3921 karma points c-trib
    Nov 18, 2009 @ 16:56
    Dan
    0

    Thanks Chris, regarding the first solution, I've tried changint this to:

    <xsl:if test="@level = 1">
                <xsl:attribute name="class"><xsl:text>tree</xsl:text></xsl:attribute>
    </xsl:if>

    But this doesn't work either.  I'm absolutely useless at XSLT (as you might have gathered!) so I appreciate your patience!

    I also tried the second solution but I get an error - presumably because the new parameter is defined in the wrong place?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <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="sitemap">
    <xsl:call-template name="drawNodes"> 
    <xsl:with-param name="first" select="1"/>
    <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@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="$first = 1">   
                <xsl:attribute name="class">tree</xsl:attribute>   
            </xsl:if>
            <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForSitemap]">
                <li> 
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        <xsl:value-of select="@nodeName"/>
                    </a> 
                    <xsl:if test="count(./node [string(./data [@alias='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>

    Thanks

  • Dan 1288 posts 3921 karma points c-trib
    Nov 18, 2009 @ 16:58
    Dan
    0

    Dan's solution works a treat.  Thanks everyone.

Please Sign in or register to post replies

Write your reply to:

Draft