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 28, 2012 @ 09:05
    Nicolai Sørensen
    0

    If (and / or) and "arrays"

    As having programmed everything else than Xslt I stumble my way to solve the things i need done :D It has taken me hours, and maybe I should have asked here a bit earlier, but one is stubbern, haha ;)

    First problem: is it only AND that can be used in IF-statements and not "this OR that"?

    Second problem:
    It is proberbly basics I missed out on, but I wanted to have a holder (Items) I could put names into and then do something like this:

    <xsl:for-each select="shop:GetCurrentCategory()/ancestor::*">
    <xsl:for-each select="$holder">
    <xsl:if test="contains(id,Item)">
    </xsl:if>
    </xsl:for-each>
    </xsl:for-each> 

    I tried placing id into a variable and to me it seems that id and Item must be different type since they are taken as different even if the are the same when printed.

  • Chriztian Steinmeier 2757 posts 8511 karma points MVP 6x admin c-trib
    Aug 28, 2012 @ 09:16
    Chriztian Steinmeier
    0

    Hi Nicolai,

    I know that stubborn feeling :-)

    You can use an or operator perfectly fine, e.g.:

    <xsl:if test="(someProperty = 'value') or (someProperty = 'anotherValue')"> ... </xsl:if>

     

    In your second example there are too many unknown factors for me to answer correctly - I don't know what $holder contains, so I can't tell you if the value of $holder/id contains the value of $holder/Item (which is what the contains() function does - it works on strings, so the processor will essentially cast the arguments for you).

    The way you've written the loops, the inner loop will not be able to access data from the outer loop... but you may have just shortened some of it for the sake of this sample? 

    /Chriztian

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

    Hey Chriztian and thx for a fast reply!

    Here is the the "holder"

    <!-- Array | Categories with 24h delivery -->
    <xsl:variable name="delivery_24">
    <Item>
    <xsl:value-of select="$c1"/>
    </Item>
    <Item>
    <xsl:value-of select="$c2"/>
    </Item>  
    </xsl:variable>

    <xsl:param name="c1">1.1</xsl:param>
    <xsl:param name="c1">2.1</xsl:param> 
    <xsl:param name="array" select="msxsl:node-set($delivery_24)"/>
    <!-- end of Array -->

    And here is the loop:

    <xsl:for-each select="shop:GetCurrentCategory()/ancestor::*">
    <xsl:for-each select="$holder">
    <xsl:variable name="catID" select="id"/>
    <xsl:if test="contains($catID,Item)">
    <!-- DO SOMETHING -->
    </xsl:if>
    </xsl:for-each>
    </xsl:for-each> 

  • Chriztian Steinmeier 2757 posts 8511 karma points MVP 6x admin c-trib
    Aug 28, 2012 @ 10:02
    Chriztian Steinmeier
    0

    Hi Nicolai,

    Ok - so now I think I know what you're trying to do, but there's a couple of gotchas. The way you set your catID variable, it takes its value from the current item in the $holder nodeset (which I assume is actually the $array variable?) - in other words: no value. If you move it out of the inner loop it will take the value from the current "ancestor" ... BUT: You need to "think different" - here's how I'd do this:

    <xsl:variable name="delivery_24">
        <Item>1.1</Item>
        <Item>2.1</Item>
    </xsl:variable>
    
    <xsl:variable name="holder" select="msxsl:node-set($delivery_24)" />
    <xsl:variable name="category" select="shop:GetCurrentCategory()" />
    
    <!-- Find all the ancestor categories having an id that matches one in $holder -->
    <xsl:for-each select="$category/ancestor::*[id = $holder/Item]">
        <!-- Do something -->
    </xsl:for-each>

    /Chriztian

     

     

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

    I really need to buy the book :D Your code is perfect and simple.

    One thing though, most of the Items put into $holder is "1.1 xxxx". Will the blank space make problems, cause it seems I can't get a result with the new holder contents.

  • Chriztian Steinmeier 2757 posts 8511 karma points MVP 6x admin c-trib
    Aug 28, 2012 @ 10:31
    Chriztian Steinmeier
    0

    I was assuming there was a 1:1 relationship between the id and the Item values - if that's not the case you just need to identify what they are - i.e., how do they relate? There's usually lots of ways to make sure you compare the right pieces, using contains(), substring() or another function...  

    /Chriztian

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

    They should be identical but for some reason they aren't seen that way.

  • Chriztian Steinmeier 2757 posts 8511 karma points MVP 6x admin c-trib
    Aug 28, 2012 @ 10:51
    Chriztian Steinmeier
    0

    Hi Nicolai,

    That sounds weird - you could try wrapping the normalize-space() function around just to check:

    <xsl:for-each select="$category/ancestor::*[normalize-space(id) = normalize-space($holder/Item)]">

    - But otherwise I'd suggest you dump the ids into to a textarea (using <xsl:copy-of select="$category/ancestor::*/id" />) and see if there's anything weird with the white-space characters...

    /Chriztian

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

    Slap me hard on the cheak with a tuna! We should have made check for unfocused coder, as I had a capital letter in an Item, man I hate being the new guy all aover again :)

    It works perfect now, thank you so much Chriztian

  • Chriztian Steinmeier 2757 posts 8511 karma points MVP 6x admin c-trib
    Aug 28, 2012 @ 11:18
    Chriztian Steinmeier
    0

    Ha :-)

    No worries - great you got it working.

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft