Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Nov 08, 2011 @ 11:28
    Martin
    0

    Get nodeName in XSLT

    Hi Im trying to pick out a nodeName and apply a class to it without any success.

    Ive checked for upper and lowercase differences.

    Any help would be grateful.

    Martin

              <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc and @nodeName='KnowHow']">
                <xsl:attribute name="class">
                  <xsl:text>knowhow</xsl:text>
                </xsl:attribute>
              </xsl:if>
  • Dan Okkels Brendstrup 101 posts 197 karma points
    Nov 08, 2011 @ 11:46
    Dan Okkels Brendstrup
    0

    Just to be sure, you are going for the actual node name, and not the nodeTypeAlias? Otherwise it'd simply be:

    <xsl:if test="$currentPage/ancestor-or-self::KnowHow">
    ...
    </xsl:if>

    If it's the node name you are going for, can you do a dump of the output and paste it in this thread? E.g.:

    <textarea>
        <xsl:copy-of select="."/>
    </textarea> 
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 08, 2011 @ 11:48
    Dirk De Grave
    0

    Sounds ok to me, have you tried...

     <xsl:iftest="$currentPage/ancestor-or-self::*[@isDoc][@nodeName='KnowHow']">
    <xsl:attributename="class">
    <xsl:text>knowhow</xsl:text>
    </xsl:attribute>
    </xsl:if>

    (BTW: you wouldn't want to select a page based on the name in the content tree, it might change and your xslt will *break*)

    Cheers,

    /Dirk

  • Martin 278 posts 662 karma points
    Nov 08, 2011 @ 12:06
    Martin
    0

    Sorry I should maybe explain a bit more.

    I have my navigation thats has a page called KnowHow. I would like to target this pages <li> and apply a class to it.

    Thanks Dan, but still now joy.

    My xslt is below

    <xsl:template match="/">  
        <!-- Root Node -->
        <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::root" />
        <!-- Homepage -->
        <xsl:variable name="homeNode" select="$rootNode/Home [@isDoc]" />    
        <ul>
          <li>
            <!--
            Add the CSS class 'selected' if the homeNode ID matches our
            currentPage node ID
            -->
            <xsl:if test="$homeNode/@id = $currentPage/@id">
              <xsl:attribute name="class">
                <xsl:text>selected</xsl:text>
              </xsl:attribute>
            </xsl:if>       
            <!--Create a link to the homepage -->
            <href="{umbraco.library:NiceUrl($homeNode/@id)}">
              <xsl:value-of select="$homeNode/@nodeName" />
            </a>
          </li>      
          <!--
          For each child node of the homeNode that is a document (isDoc)
          and the level is 2
          and the property umbracoNaviHide is NOT 1 (true/checked)
          -->
          <xsl:for-each select="$homeNode/* [@isDoc and @level = 2 and string(umbracoNaviHide) != '1']"
            <li>
              <!--
              Add the CSS class 'selected' if the currentPage or parent nodes (up the tree to the root)
              ID matches our current node's ID in the for each loop
              -->
              <xsl:if test="$currentPage/ancestor-or-self::* [@isDoc]/@id = current()/@id">
                <xsl:attribute name="class">
                  <xsl:text>selected</xsl:text>
                </xsl:attribute>
              </xsl:if>          
              <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc and @nodeName='KnowHow']">
                <xsl:attribute name="class">
                  <xsl:text>knowhow</xsl:text>
                </xsl:attribute>
              </xsl:if>          
              <!-- Create the link -->
              <href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName" />           
              </a>                  
              <!--DROP DOWN NAVIGATION-->
              <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">     
                <ul
                  <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1']"
                    <li><href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></li
                 </xsl:for-each>         
                </ul>
              </xsl:if>          
            </li>      
          </xsl:for-each>      
        </ul>
      </xsl:template>
  • Dan Okkels Brendstrup 101 posts 197 karma points
    Nov 08, 2011 @ 14:53
    Dan Okkels Brendstrup
    0

    If your node is actually called "KnowHow", then I don't see why this wouldn't work. I agree with Dirk, though, that basing anything in XSLT on the name of a specific node is very fragile, since the value can so easily change.

    I assume what you are trying to achieve is a different styling of that specific section in the Menu? Does the KnowHow section by any chance contain nodes of a different doctype than the other sections? In that case you could find a more robust way to match that section in the XPath.

    Be aware that the "knowhow" class will override the "selected" class if both statements are valid for the given node. This might be a more robust way to do it:

    <xsl:for-each select="$homeNode/*[@level = 2][umbracoNaviHide != 1]">
      <li class="{@urlName}">
        <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = current()/@id">
          <xsl:attribute name="class"><xsl:value-of select="concat(@urlName, ' selected'"/></xsl:attribute>
        </xsl:if>
        <!-- Create the link -->
        <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a>
        <!--DROP DOWN NAVIGATION-->
        <xsl:if test="*[umbracoNaviHide != 1]">
          <ul>
            <xsl:for-each select="*[umbracoNaviHide != 1]">
              <li><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></li>
            </xsl:for-each>
          </ul>
        </xsl:if>
      </li>
    </xsl:for-each>

    You're still depending on the node name, since that forms the bases of the @urlName, but the @urlName can be "frozen" via an umbracoUrlName property on the page to make it a bit more bulletproof. And you'll end up with a <li class="knowhow selected"/> and can style according to both properties.

  • Martin 278 posts 662 karma points
    Nov 08, 2011 @ 17:27
    Martin
    0

    Thanks Dan, that worked a treat.

Please Sign in or register to post replies

Write your reply to:

Draft