Copied to clipboard

Flag this post as spam?

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


  • Accusoft 58 posts 109 karma points
    May 17, 2011 @ 15:31
    Accusoft
    0

    How do I access custom fields in XSLT loop?

    I've got a custom field called relatedProducts in one of my articles.  This field can be empty or it can contain a list of IDs (i.e. 4,9,10).  I need to execute code similar to the following:

    <xsl:for-each select="$currentPage/child::*
     [(@parentID = $parentId) and ((contains(current()/relatedProducts, 
    $productId)) or (string-length($productId) = 0))]">
    </xsl:for-each>

    The problem is "current()/relatedProducts" doesn't exist at that point in the loop.  I can use "current()/relatedProducts" inside the loop to display, but not within the loop parameters.  How can I do this comparison?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 17, 2011 @ 22:38
    Chriztian Steinmeier
    0

    Hi Chris,

    Inside the loop, current() gives you the current item in the loop - in the select attribute, current() will give you the node that "contains" the for-each, i.e.: if your for-each is inside the "root" template, it'll give you the <macro> element (match="/" is executed on the <macro> element).

    I'd write your expression like this:

    <xsl:for-each select="
        $currentPage/*
        [@parentId = $parentId]
        [contains(relatedProducts, $productId) or not(normalize-space($productId))]"
    >
        <!-- do stuff -->
    </xsl:for-each>

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft