Copied to clipboard

Flag this post as spam?

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


  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 11:38
    Nicolai Sørensen
    0

    node-set Items in loops

    I am trying to pass Items to a node-set while looping through ancerstors, but for some reason I only get the first recorded Item, and it was the last one I was looking for, the closest ancestor to where I am.

    <xsl:variable name="delivery">

    <xsl:for-each select="$CurrentCategory/ancestor-or-self::*">

    <xsl:if test="id = $holder24/Item">

    <Item>2</Item>

    </xsl:if>

    <xsl:if test="id = $holder48/Item">

    </xsl:if>

    <xsl:if test="id = $holder72/Item">

    <Item>3</Item>

    </xsl:if>

    </xsl:for-each>

    </xsl:variable>

    <xsl:variable name="holder_delivery" select="msxsl:node-set($delivery)" />

  • Chriztian Steinmeier 2750 posts 8456 karma points MVP 6x admin c-trib
    Aug 30, 2012 @ 11:54
    Chriztian Steinmeier
    0

    Hi Nicolai,

    The only place where you can use the backwards-selection order of the ancestor:: and ancestor-or-self:: axes, is in a predicate within the same "step" of an XPath selection, e.g.:

    <xsl:variable name="firstAncestor" select="$currentPage/ancestor::*[1]" />

    or the one referred to as a recursive lookup for the first ancestor with a value in a specific property:

    <xsl:variable name="pageTitle" select="$currentPage/ancestor-or-self::*[normalize-space(metaPageTitle)][1]/pageTitle" />

     

    If you only want to look at the first ancestor, you can use the parent:: axis (abbreviated as "..") instead:

    <xsl:variable name="parentCategory" select="$CurrentCategory/.." />
    
    <xsl:if test="$parentCategory[id = $holder24/Item]"> ... </xsl:if>

    /Chriztian

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 11:59
    Nicolai Sørensen
    0

    Hey again Chriztian ;)

    problem is that I am comparing it against 3 node-sets to se which one is closest and the last value passed if any will be the one I need.

  • Chriztian Steinmeier 2750 posts 8456 karma points MVP 6x admin c-trib
    Aug 30, 2012 @ 12:14
    Chriztian Steinmeier
    0

    I guess you should be able to stuff all the delivery options into a single set - a la:

    <xsl:variable name="deliveryOptionsProxy">
        <Item id="XXX" duration="24h" />
        <Item id="YYY" duration="48h" />
        <Item id="ZZZ" duration="72h" />
    </xsl:variable>
    <xsl:variable name="deliveryOptions" select="make:node-set($deliveryOptionsProxy)" />

    And then select like this: 

    <!-- First ancestor that has an id matching one in the deliveryOptions set -->
    <xsl:variable name="firstMatch" select="$CurrentCategory/ancestor-or-self::*[id = $deliveryOptionsProxy/Item/@id][1]" />
    
    <!-- Take the value of the duration attribute from the matching Item -->
    <xsl:variable name="deliveryTime" select="$deliveryOptions/Item[@id = $firstMatch/id]/@duration" />

    Does that make sense? It requires the ids to be unique across delivery options, but I guess they are, right?

    /Chriztian

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 12:24
    Nicolai Sørensen
    0

    It makes alot of sense, and yes the id's are unique.

    Just tested it and I get a: Prefix 'make' is not defined

  • Chriztian Steinmeier 2750 posts 8456 karma points MVP 6x admin c-trib
    Aug 30, 2012 @ 12:28
    Chriztian Steinmeier
    0

    Ha - that's just because it isn't - only my code can use that :-)

    You can just use msxsl:node-set() as you're used to...

    /Chriztian

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 12:33
    Nicolai Sørensen
    0

    Was thinking that and it removed the error and it saved. Somehow I ended up with a parse error instead.

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 12:35
    Nicolai Sørensen
    0

    this one makes the parse error :

    <xsl:variable name="firstMatch" select="$CurrentCategory/ancestor-or-self::*[id = $deliveryOptionsProxy/Item/@id][1]" />

  • Chriztian Steinmeier 2750 posts 8456 karma points MVP 6x admin c-trib
    Aug 30, 2012 @ 12:46
    Chriztian Steinmeier
    0

    Ah yes - sorry about that, it should be "deliveryOptions" instead... 

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 12:54
    Nicolai Sørensen
    0

    I get no results and it should have 2 passed from the first for-each.

    <xsl:variable name="deliveryOptionsProxy">

    <xsl:for-each select="$CurrentCategory/ancestor-or-self::*">

    <xsl:if test="id = $holder24/Item">

    <Item id="d24" duration="2"/>

    </xsl:if>

    <xsl:if test="id = $holder48/Item">

    </xsl:if>

    <xsl:if test="id = $holder72/Item">

    <Item id="d72" duration="3"/>

    </xsl:if>

    </xsl:for-each>

    </xsl:variable>

    <xsl:variable name="deliveryOptions" select="msxsl:node-set($deliveryOptionsProxy)" />

    <xsl:variable name="firstMatch" select="$CurrentCategory/ancestor-or-self::*[id = $deliveryOptions/Item/@id][1]" />

    <xsl:variable name="deliveryTime" select="$deliveryOptions/Item[@id = $firstMatch/id]/@duration" />

    <xsl:value-of select="$deliveryTime"/>

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 12:55
    Nicolai Sørensen
    0

    is it the id from the items that are making the problem as we search for ancestor id = Item id?

  • Chriztian Steinmeier 2750 posts 8456 karma points MVP 6x admin c-trib
    Aug 30, 2012 @ 13:06
    Chriztian Steinmeier
    0

    Hi Nicolai,

    Actually, the idea of this approach was that you had ALL possible options in that deliveryOptions variable once and for all - collecting all the options from $holder_24 $holder_72 etc...

    Then you'd be able to select without using a for-each ...

    What are the ids on those ancestors? Are they not references to a delivery method of some sort?

    /Chriztian

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 13:10
    Nicolai Sørensen
    0

    ah so I have to merge the node-sets using id's?

  • Nicolai Sørensen 42 posts 66 karma points
    Aug 30, 2012 @ 13:14
    Nicolai Sørensen
    0

    there is no reference to how long time it will take to recieve the delivery, thats why I try to add it by a category and whats below it.

Please Sign in or register to post replies

Write your reply to:

Draft