Copied to clipboard

Flag this post as spam?

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


  • dave2112 17 posts 35 karma points
    Oct 24, 2013 @ 10:47
    dave2112
    0

    Using descendant or self

    Hi,

    I have created a little product listing in Umbraco, my node structure is like this...

    - Categories
    ---category_one
    -----sub_category
    -----sub_category
    ---category_two
    ---category_three

    - Products (node id 2878)
    ---product
    ---product
    ---product

    The product document type has properties which say which categories the product belongs to. These are done using a content picker so the user can just select the categories applicable. I am having some problems writing the XSLT.

    On the category page I need to have a products listing. This was easy enough but on the category page I want the products that are in the sub categories to show too. So if I am on "categoryone" page I want to show the products that are in the category plus the ones in the sub categories.

    I have tried to use descendant or self but I can't get it to work. Here is my code:

    <xsl:for-each select="umbraco.library:GetXmlNodeById(2878)/descendant-or-self::product [$currentPage/descendant-or-self::category [@id = categoryOne]]">

    Could anyone point me in the right direction please?

    Thanks in advance 

  • Kim Nedergaard 37 posts 144 karma points
    Oct 24, 2013 @ 20:24
    Kim Nedergaard
    1

    Hi Dave,

    I've had the same challenge in a project. I solved it by making an variable containing all the wanted categorie IDs sepereated by komma - and then used contains in my xpath.

    It goes something like this:

    <xsl:variable name="currentCategories">
    <xsl:for-each select="$currentPage/descendant-or-self::category"><xsl:if test="position() = 1">,</xsl:if><xsl:value-of select="@id"/>,</xsl:for-each>
    </xsl:variable>
    
    <xsl:for-each select="umbraco.library:GetXmlNodeById(2878)/descendant-or-self::product [contains($currentCategories,','+categoryOne+',')]">
    

    *I assume categoryOne is your category-property on your product? :)

    Hope this helps.

    -Kim

  • dave2112 17 posts 35 karma points
    Oct 24, 2013 @ 21:02
    dave2112
    0

    Hi Kim

    Thanks for the suggestion, I think that could do the trick :-)

    Thanks again!

  • Kim Nedergaard 37 posts 144 karma points
    Oct 24, 2013 @ 22:10
    Kim Nedergaard
    0

    You're welcome :)

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 24, 2013 @ 22:31
    Chriztian Steinmeier
    0

    Hi Dave,

    I'm not totally sure how your categoryOne property works - is it a plain Content Picker (so only a single category can be picked) or is it maybe a Multi-Node Tree Picker for selecting multiple categories?

    I'm guessing it's a Content Picker, so you should be able to do it like this:

    <!-- Grab all product nodes -->
    <xsl:variable name="products" select="umbraco.library:GetXmlNodeById(2878)//product" />
    
    <!-- Select product nodes where the productOne property points to the current category or one of its descendants -->
    <xsl:for-each select="$products[categoryOne = $currentPage/descendant-or-self::category/@id]">
       ...
    </xsl:for-each>
    

    (This assumes that the category + subcategory nodes have the alias category - but you can of course change that very easily)

    /Chriztian

  • dave2112 17 posts 35 karma points
    Oct 25, 2013 @ 15:08
    dave2112
    0

    Thank you Kim and Chriztian - both of those methods worked for me

  • 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