Copied to clipboard

Flag this post as spam?

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


  • Frederik T 242 posts 373 karma points
    Nov 25, 2011 @ 11:01
    Frederik T
    0

    Comma seperated lists and looping

    I have searched around for how to handle comma seperated lists, but they all look more advanced (i think) than what i need. Also i mostly dont understand the solutions 100%.

    Let me explain it very quick first:

    I have one page with a simple UL list that contains a bunch of links. Categories. Those links have a series of number appended to the url, so the result is: www.mysite.com/categorylist/itemlist?categoryId=118,123,113,108,103
    Other links have fewer or more numbers.

    That "itemlist" (its just a made up name for this case) is supposed to show the contents from the chosen category and the xslt is supposed to recieve that series of numbers, and looks for all the items that contains those numbers from a webservice xml.

    I know how to do everything EXCEPT splitting up the id's and also "for-eaching" for items that contains those numbers.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 25, 2011 @ 11:56
    Chriztian Steinmeier
    2

    Hi Frederik,

    To split the list, you can use umbraco.library:Split() which will give you a values element with a value for every value (ahem :-) in the list - you can throw that in a for each right away:

     
    
    <!-- Grab a reference to the Top level node in the site -->
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
    <!-- Select the parent of all the nodes that should be included in the search for categories -->
    <xsl:variable name="source" select="$siteRoot/News" />
    
    <!-- Get the list from QueryString -->
    <xsl:variable name="itemlist" select="umbraco.library:RequestQueryString('categoryId')" />
    
    <!-- Split and Go -->
    <xsl:for-each select="umbraco.library:Split($itemlist, ',')//value">
        <xsl:apply-templates select="$source//*[@isDoc][contains(concat('|', categories, '|', concat('|', current(), '|')]" />
    </xsl:for-each>
    
    <!-- Template for a node that contains the categoryId -->
    <xsl:template match="*[@isDoc]">
        <p>
            <xsl:value-of select="@nodeName" />
        </p>  
    </xsl:template>
    

    (The fiddly stuff with the concat() and the pipes are to ensure that an item with the category 1234 doesn't get selected when the categories12, 23, 234 etc. are selected.)  

    /Chriztian

  • Frederik T 242 posts 373 karma points
    Nov 25, 2011 @ 12:45
    Frederik T
    0

    Thanks! I think its almost there, the only difference being im not looking for umbraco nodes but xml nodes from a webservice.

    Normally it would look like this:

    <xsl:for-each select="$Service/ArrayOfJob/Job">
        <li>item name for example goes here</li>
      </xsl:for-each>

    Service is the webservice, and the nodes in that xml each contain a different category id. And i need a list of all those coresponding to the comma list.

    so this is what im at now:

    <ul data-role="listview" data-inset="true">
      <li data-role="list-divider"><xsl:value-of select="$categoryName"/></li>
      <xsl:for-each select="$Service/ArrayOfJob/Job">
        <xsl:if test="Categories/JobCategory/CategoryID = umbraco.library:Split($categoryCsl, ',')//value">
          <li><a><xsl:attribute name="href">#</xsl:attribute><xsl:value-of select="Name"/><span class="ui-li-count">10</span></a></li>
        </xsl:if>
      </xsl:for-each>
    </ul>

    which, doesnt work. I am sure im missing something somewhere... maybe im just navigating the xml wrong. it does recieve the "categoryName" and display it so something is at least working. the list is just empty. Gotta experiment some more but suggestions are helpful.

    Oh and excuse the lack of proper code formatting, but the forum is acting up.

  • Frederik T 242 posts 373 karma points
    Nov 25, 2011 @ 12:52
    Frederik T
    0

    Sigh, cant edit my post.

    but a correction: my code DOES work, but it only takes the first ID in the comma string.

  • Frederik T 242 posts 373 karma points
    Nov 25, 2011 @ 15:33
    Frederik T
    0

    Sorry for bumping, but the issue is becoming increasingly urgent, and i believe the error is a mere oversight but my knowledge about this particular subject is lacking to say the least.

    Also, that editing and other function on this forum doesnt work isnt helping. Sorry about that.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 25, 2011 @ 23:05
    Chriztian Steinmeier
    1

    Hi Frederik,

    The way you perform the test, the processor will take the first value element as the right hand side of the test. Also: you're doing the same Split() on every iteration - waste of cycles :-)

    First, put the result of the Split() into a handy variable; Then use that variable in a slightly modified version of the test:

    <xsl:variable name="values" select="umbraco.library:Split($categoryCsl, ',')/values" />
    
    <ul data-role="listview" data-inset="true">
        <li data-role="list-divider">
            <xsl:value-of select="$categoryName" />
        </li>
        <xsl:for-each select="$Service/ArrayOfJob/Job">
            <xsl:if test="Categories/JobCategory[CategoryID = $values/value]">
                <li>
                    <a href="#">
                        <xsl:value-of select="Name" />
                        <span class="ui-li-count">10</span>
                    </a>
                </li>
            </xsl:if>
        </xsl:for-each>
    </ul>

    Hope it helps - otherwise, come back again!

    /Chriztian 

  • Frederik T 242 posts 373 karma points
    Dec 01, 2011 @ 09:47
    Frederik T
    0

    Thank you for your reply, this is what i have:

     

        <xsl:variable name="values" select="umbraco.library:Split($categoryCsl, ',')/value" />

    <xsl:template match="/">
     
      
    <ul data-role="listview" data-inset="true">
      <li data-role="list-divider">
        <xsl:value-of select="$categoryName" />
      </li>
      <xsl:for-each select="$Service/ArrayOfJob/Job">
        <xsl:if test="Categories/JobCategory[CategoryID = $values/value]">
          <li>
            <a href="#">
              <xsl:value-of select="Name" />
              <span class="ui-li-count">10</span>
            </a>
          </li>
        </xsl:if>
      </xsl:for-each>
    </ul>


    Sorry for the lack of formatting, but the forum wont cooperate. Anyway, ive tested the values of "value" and everything comes out correctly. But the code above does not work. somewhat, ive narrowed it down to the
    [CategoryID = $values/value] because if i set the CategoryID to a hardcoded value it spits out the correct data. So the solution must be very close.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 01, 2011 @ 10:01
    Chriztian Steinmeier
    1

    Hi Frederik,

    Note that I put values (plural) on the end of the call to Split() - you're using the singular value.

    Split() returns an XML chunk like this:

    <values>
       <value>1200</value>
       <value>4563</value>
       <!-- etc. -->
    </values>

    So by assigning the $values variable directly to the "values" element enables using XPaths like "$values/value" to select the stuff you need.

    /Chriztian 

  • Frederik T 242 posts 373 karma points
    Dec 01, 2011 @ 11:03
    Frederik T
    0

    I tried that, and it didnt output anything, thats why i changed it to /value, which did output the correct values. But the loop doesnt work. Very odd indeed... i tried for having two "for-each" loops, which didnt worker either. Im sure i missed something simple...

  • Frederik T 242 posts 373 karma points
    Dec 01, 2011 @ 11:05
    Frederik T
    0

    Ok, looks like it is working, to be honest im not 100% of the reason, but this is what i came up with:

    <xsl:variable name="values" select="umbraco.library:Split($categoryCsl, ',')//value" />

    <xsl:template match="/">
      
    <ul data-role="listview" data-inset="true">
      <li data-role="list-divider">
        <xsl:value-of select="$categoryName" />
      </li>
      <xsl:for-each select="$Service/ArrayOfJob/Job">
        <xsl:if test="Categories/JobCategory[CategoryID = $values]">
          <li>
            <a>
              <xsl:attribute name="href">/students/jobkategorier/jobliste/job.aspx?jobid=<xsl:value-of select="JobID" /></xsl:attribute>
              <xsl:value-of select="Name" />
              <span class="ui-li-count">10</span>
            </a>
          </li>
        </xsl:if>
      </xsl:for-each>
    </ul>

    either way, looks like it ended up with some xpath confusion, but regardless i wouldnt have been able to come this far without help, so i deeply appreciate it.

    I am glad that there are so many willing to help "noobs" like me :)

Please Sign in or register to post replies

Write your reply to:

Draft