Copied to clipboard

Flag this post as spam?

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


  • dandrayne 1138 posts 2262 karma points
    May 20, 2009 @ 13:16
    dandrayne
    0

    Getting a unique list from comma separated values in xslt

    Hi Folks

    I wonder if anyone can help on this one - I'm using the v3 blog package on a site, and am trying to create a subnavigation list of categories on the left hand side. The categories come out attached to each entry as a comma separated list within data [@alias='categories'].

    Here's the code that spits out a list of all categories used

    [code]

    ]>

    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library">






    Blog Categories









    [/code]


    And here's a bit of code I use on another project for getting a unique list out of a list of values

    [code]

    ]>

    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:tagsLib="urn:tagsLib"
    xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
    exclude-result-prefixes="msxml umbraco.library tagsLib Exslt.ExsltStrings">









    [/code]


    And just for the sake of completion, here's the jquery code to get rid of the duplicates in my category list.

    [code]
    $(".blog_cats").children("li").each(function(x){
    test = false;
    b = a[x] = $(this).text();
    for (i=0;i<>
    if (b ==a[i]) test =true;
    }
    if (test) $(this).remove();
    })

    [/code]



    My question is basically how I can get a list of unique values from the separate comma separated nodes? As the page and category list gets bigger, a noticeable lag occurs before the jquery kicks in (waits for document load). A non js solution is what's needed here.

    As shown above, I can get uniques from a straight list, but I think I need to get all the csv values into a 'master' list, sort that and then use the above xslt [not(.=following::DocProperty)]. To frustrate matters further, I'd hoped that http://umbraco.org/documentation/books/extending-xslt-with-c#-or-javascript might help but it's kaput.

    Anyone know of a good way to do this? Let me know if you need more clarification!

    Cheers,
    Dan

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 20, 2009 @ 17:01
    Douglas Robar
    0

    The way I split is much like I'd do for looking at the @path, which is also comma-separated. One way to handle that is shown in the ps:isInList() function in my navigation script. This is a c# function embedded right in the xslt macro. You might be able to steal (errr, "re-purpose") my function for your uses. See [url]http://forum.umbraco.org/yafpostst7239xslt-Subnavigation.aspx[/url]

    Or, use a recursive template and the substring-before() or substring-after function. You'll want to concatinate a leading or trailing comma to the string to handle the first or last item in the list. Just use the concat() function for that.

    Or, use the EXSLT split() function. Read more at [url]http://www.exslt.org/str/functions/split/str.split.html[/url]



    Let us know what you come up with for the next fellow that has the same question!

    cheers,
    doug.

  • dandrayne 1138 posts 2262 karma points
    May 20, 2009 @ 18:40
    dandrayne
    0

    Thanks for the options, doug. Great stuff!

    I decided to try the EXSLT Split function, and have managed to get all the categories into a node set of tokens, although I can't seem to access them in the traditional way. xsl:copy-of gives me a list of nodes, but I cant for-each through them

    [code]


    ]>
    <>
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon"
    xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes"
    xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath"
    xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions"
    xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library
    Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath
    Exslt.ExsltRegularExpressions Exslt.ExsltSets Exslt.ExsltStrings">







    Blog Categories










    [/code]


    So does what it should (I thinK) but I can't loop through with the command in the above xslt. Am i doing something wrong here?

    Cheers

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 21, 2009 @ 15:05
    Douglas Robar
    0

    Off the top of my head, what if you try one of this for-each loop instead?

    [code]

Please Sign in or register to post replies

Write your reply to:

Draft