Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1287 posts 2744 karma points
    Apr 18, 2013 @ 22:14
    Amir Khan
    0

    Add class based on parent node

    I'm trying to add class to my search results based on the results parent node id. I'm trying the following with no luck, any ideas?

    <!-- add class based on site -->
    <xsl:if test="ancestor-or-self::node/@id = 1077">
    <span class="myClass">
    yeah!
    </span>
    </xsl:if>
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Apr 18, 2013 @ 22:35
    Chriztian Steinmeier
    0

    Hi Amir,

    If you're inside a for-each or a match template, do this:

    <!-- Test a built-in attribute (works in both schemas) -->
    <xsl:if test="@parentID = 1077">
        <span class="myClass">Yeah</span>
    </xsl:if>
    
    <!-- Test a custom property -->
    <xsl:if test="../header = 'My Header'">
        <!-- do stuff -->
    </xsl:if>
    
    <!-- Test a custom property (old schema) -->
    <xsl:if test="../data[@alias = 'header'] = 'My Header'">
        <!-- do stuff -->
    </xsl:if>
    

    If you're inside the root template (match="/") you need to base the XPath off of $currentPage:

    <xsl:if test="$currentPage/@parentID = 1077">
        <!-- do stuff -->
    </xsl:if>
    
    <xsl:if test="$currentPage/../header = 'My Header'">
        <!-- do stuff -->
    </xsl:if>

    Hop that helps!

    /Chriztian

  • Amir Khan 1287 posts 2744 karma points
    May 01, 2013 @ 20:18
    Amir Khan
    0

    Hi Chriztian, oddly this is only working for the first result? I'm adding it around line 350 inside this loop:

     

              <xsl:for-each select="$matchedNodes">

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 02, 2013 @ 08:35
    Chriztian Steinmeier
    0

    Hi Amir,

    I think I misunderstood you then - you're trying to check if a search result has a specific ancestor (not parent?) - in that case:

    <xsl:for-each select="$matchedNodes">
        <xsl:if test="ancestor::*[@id = 1077]">
            <span class="myClass">
                <!-- ... -->
            </span>
            <!-- ... -->
        </xsl:if>
    </xsl:for-each>

    (works with both the old and the new schema, btw.)

    - If the node being checked could itself be the node with id 1077, use the ancestor-or-self:: axis instead.

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft