Copied to clipboard

Flag this post as spam?

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


  • Dan 1250 posts 3747 karma points admin c-trib
    May 24, 2010 @ 15:27
    Dan
    0

    List all sub-nodes only where parent nodeId matches

    Hi,

    I have a structure like this:

    - Master home
    - - en
    - - - stockists
    - - - - uk
    - - - - - london
    - - - - - - shop1
    - - - - - - shop2
    - - - - - manchester
    - - - - - - shop3
    - - - - - - shop4
    - - - - - bristol
    - - - - - - shop5
    - - - - - - shop6
    - - - - italy
    - - - - - rome
    - - - - - - shop7
    - - - - - milan
    - - - - - - shop8

    etc.

    I have a macro on the stockists page which picks out all stockists, listing their country then city, then stockist name, then address details.  It works great, but I'm trying to add a filter which shows only nodes from a particular country, when the nodeId of the country is passed into the macro XSLT file as a form post variable.

    The code I have so far is this:

    <xsl:param name="currentPage"/>

    <xsl:variable name="countryId">
    <xsl:choose>
    <xsl:when test="string(umbraco.library:RequestForm('country')) != ''">
    <xsl:value-of select="string(umbraco.library:RequestForm('country'))" />
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="''"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <xsl:template match="/">

    <ul>
    <xsl:for-each select="$currentPage/descendant::node [string(data [@alias='umbracoNaviHide']) != '1']">
    <li>
    <xsl:if test="@nodeTypeAlias = 'Country'">
    <xsl:attribute name="class"><xsl:text>country</xsl:text></xsl:attribute>
    </xsl:if>
    <xsl:choose>
    <xsl:when test="@nodeTypeAlias = 'Stockist'">
    <xsl:value-of select="@nodeName"/><br />
    </xsl:when>
    <xsl:otherwise>
    <span class="node-name"><xsl:value-of select="@nodeName"/></span><br />
    </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="data [@alias = 'address1'] != ''">
    <xsl:value-of select="data [@alias = 'address1']"/><br />
    </xsl:if>
    <xsl:if test="data [@alias = 'address2'] != ''">
    <xsl:value-of select="data [@alias = 'address2']"/><br />
    </xsl:if>
    <xsl:if test="data [@alias = 'telephoneNumber'] != ''">
    <xsl:value-of select="data [@alias = 'telephoneNumber']"/><br />
    </xsl:if>
    </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

    I'm not sure how to get it to pick out just the particular nodes from the given countryId.  Do I create something inside the for-each which traverses back to the root node, then forwards by level to the country node and does a comparison, or can I put something inside the main <for-each select> which only selects the correct node to begin with?

    Thanks.

  • Tommy Poulsen 514 posts 708 karma points
    May 24, 2010 @ 16:02
    Tommy Poulsen
    0

    Hi Dan

    I would go for adding an additional condition to your for-each.

    Assuming your currentPage is the "stockist" node, do something like this (out of my head - soo untested)

    <xsl:for-each select="$currentPage/node[@nodeTypeAlias = 'country' and @nodeName = $countryId]/descendant::node [string(data [@alias='umbracoNaviHide']) != '1']">

    >Tommy

  • Dan 1250 posts 3747 karma points admin c-trib
    May 24, 2010 @ 16:19
    Dan
    0

    Thanks Tommy,

    I've modified that to this:

    $currentPage/node[@nodeTypeAlias = 'Country' and @id = $countryId]/descendant::node [string(data [@alias='umbracoNaviHide']) != '1']

    It works okay, except that the default for the page is that there is no countryId passed in, in which case it needs to show all country nodes.

  • Tommy Poulsen 514 posts 708 karma points
    May 24, 2010 @ 16:22
    Tommy Poulsen
    0

    The you add an "or"-part

    $currentPage/node[@nodeTypeAlias = 'Country' and ($countryId='' or @id = $countryId)]/descendant::node [string(data [@alias='umbracoNaviHide']) != '1']

     

  • Dan 1250 posts 3747 karma points admin c-trib
    May 24, 2010 @ 16:33
    Dan
    0

    Excellent, thanks Tommy!  The final selection is (for the record):

    $currentPage/node[@nodeTypeAlias = 'Country' and ($countryId='' or @id = $countryId)]/descendant-or-self::node [string(data [@alias='umbracoNaviHide']) != '1']
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies